在一个由JS文件创建的innerHTML中访问类名

huangapple go评论72阅读模式
英文:

Accessing class name in a innerHtml made in js file

问题

I am currently creating a cart with local storage.
When I add my products one by one, in the cart page, I do it with innerHtml:

productsContainer.innerHTML += `
            <div class="cart-product">
            <img src="../images/${item.tag}" alt="image product" style="height: 120px;">
            <div class="product-info-container">
                <div class="product-cross"><i class="bi bi-x"></i></div>
                <div class="product-info">
                <h4 class="product-title">${item.name}</h4>
                <h5 class="price">${item.inCart * item.price} €</h5>
                <div class="product-info-number">
                    <i class="bi bi-dash" id="bi-dash"></i>
                    <span class="quantity">${item.inCart}</span>
                    <i class="bi bi-plus" id="bi-plus"></i>
                </div>
                </div>
            </div>
        </div>
            `

The problem I have now is that I want to access my two icons (i class="bi-dash / i class="bi-plus") to increase and decrease the quantity. I tried to store them in variables with querySelector and getElementById but it doesn't work. I get a null error.

英文:

I am currently creating a cart with local storage.
When I add my products one by one, in the cart page, I do it with innerHtml :

productsContainer.innerHTML += `
            &lt;div class=&quot;cart-product&quot;&gt;
            &lt;img src=&quot;../images/${item.tag}&quot; alt=&quot;image product&quot; style=&quot;height: 120px;&quot;&gt;
            &lt;div class=&quot;product-info-container&quot;&gt;
                &lt;div class=&quot;product-cross&quot;&gt;&lt;i class=&quot;bi bi-x&quot;&gt;&lt;/i&gt;&lt;/div&gt;
                &lt;div class=&quot;product-info&quot;&gt;
                &lt;h4 class=&quot;product-title&quot;&gt;${item.name}&lt;/h4&gt;
                &lt;h5 class=&quot;price&quot;&gt;${item.inCart * item.price} €&lt;/h5&gt;
                &lt;div class=&quot;product-info-number&quot;&gt;
                    &lt;i class=&quot;bi bi-dash&quot; id=&quot;bi-dash&quot;&gt;&lt;/i&gt;
                    &lt;span class=&quot;quantity&quot;&gt;${item.inCart}&lt;/span&gt;
                    &lt;i class=&quot;bi bi-plus&quot; id=&quot;bi-plus&quot;&gt;&lt;/i&gt;
                &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
            `

The problem I have now is that I want to access my two icons (i class="bi-dash / i class="bi-plus") to increase and decrease the quantity. I tried to store them in variables with querySelctore and getElementById but it doesn't work. I get a null error.

答案1

得分: 1

看起来工作正常。

以下是在控制台中记录的元素示例:

<html>
<body>
    <div id="test"></div>

    <script>
        let testElement = document.getElementById("test");
        testElement.innerHTML += `
            <i class="bi bi-dash" id="bi-dash">bi-dash</i>
            <i class="bi bi-plus" id="bi-plus">bi-plus</i>
        `;
        let biDash = document.getElementById("bi-dash");
        let biPlus = document.getElementById("bi-plus");

        console.log(test, biDash, biPlus);
    </script>

</body>
</html>

希望这对您有帮助。

英文:

Seems to work fine.

Here's a snippet showing the elements being logged in the console:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html&gt;
&lt;body&gt;
    &lt;div id=&quot;test&quot;&gt;&lt;/div&gt;

&lt;script&gt;
    let testElement = document.getElementById(&quot;test&quot;);
    testElement.innerHTML += `
        &lt;i class=&quot;bi bi-dash&quot; id=&quot;bi-dash&quot;&gt;bi-dash&lt;/i&gt;
        &lt;i class=&quot;bi bi-plus&quot; id=&quot;bi-plus&quot;&gt;bi-plus&lt;/i&gt;
    `;
    let biDash = document.getElementById(&quot;bi-dash&quot;);
    let biPlus = document.getElementById(&quot;bi-plus&quot;);
    
    console.log(test, biDash, biPlus);
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月7日 02:21:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定