make a price reduction form div appear and disappear using a button. but I have several button in 10 card

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

make a price reduction form div appear and disappear using a button. but I have several button in 10 card

问题

I have a problem I have several cards wi
on to make a div appear in . They all have the same class but there is only one of my buttons that works on the other cards, it does nothing

英文:

I have a problem I have several cards wi
on to make a div appear in . They all have the same class but there is only one of my buttons that works on the other cards, it does nothing

<article class="grid-product">
    <div class="grid-items-product">
        <div class="grid-item-product">
            <img src="./images/sucette.jpg" alt="sucette">
        <div class="card-content">
            <button class="control-price" >
                 <i class="fa-sharp fa-solid fa-pen"></i>
            </button>
        <h3>Sucette</h3>
        <span>A partir de <strong id="price-sucette">25 €</strong> </span>
        </div>
        </div>
        <div class="grid-item-product">
            <img src="./images/cannes.jpg" alt="bonbon cannes">
        <div class="card-content">
            <button class="control-price">
                <i class="fa-sharp fa-solid fa-pen"></i>
            </button>
        <h3>Cannes</h3>
        <span>A partir de <strong id="price-cannes">25€</strong> </span>
        </div>
        </div>
        <div class="grid-item-product">
            <img src="./images/ourson.jpg" alt="bonbon oursson">
        <div class="card-content">
        <button class="control-price">
             <i class="fa-sharp fa-solid fa-pen"></i>
        </button>
        <h3>Ourson</h3>
        <span>A partir de <strong id="price-ourson">25€</strong> </span>
        </div>
        </div>
    </div>   
</article>
.form-reduction-price{
    width: 90%;
    background-color: #fff;
    display: none;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    margin: auto;
    padding-bottom: 25px;
    
}
.btn-closed{
    display: flex;
    align-self:end;
    border: none;
    font-size:16px;
    background: #fff;
}


let promoFormOpen=document.querySelector(".control-price");
promoFormOpen.addEventListener('click',()=>{ 
    document.querySelector ('.form-reduction-price').style.display="flex"        
    });
let closed=document.querySelector(".btn-closed")
     closed.addEventListener('click',()=>{
         document.querySelector('.form-reduction-price').style.display="none";

I have a problem I have several cards wi
on to make a div appear in . They all have the same class but there is only one of my buttons that works on the other cards, it does nothing

答案1

得分: 1

document.querySelector 会返回第一个匹配的元素,你可能正在寻找 querySelectorAll,以一种方式来识别每个单独的 "price reduction" 元素。

所以,你可以选择所有的按钮,如下所示:

let promoFormOpenButtons = document.querySelectorAll(".control-price");

然后为每个按钮添加事件监听器:

promoFormOpenButtons.forEach(btn => {
  btn.addEventListener('click', (e) => {
    // 做一些操作
  });
});

完整示例:

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

<!-- language: lang-js -->
let promoFormOpenButtons = document.querySelectorAll(".control-price");

promoFormOpenButtons.forEach(btn => {
  btn.addEventListener('click', (e) => {
    console.log(e.target.parentElement.querySelector('h3').textContent);
  });
});

<!-- language: lang-html -->
<article class="grid-product">
  <div class="grid-items-product">
    <div class="grid-item-product">
      <img src="./images/sucette.jpg" alt="sucette">
      <div class="card-content">
        <button class="control-price">
          <i class="fa-sharp fa-solid fa-pen"></i>
        </button>
        <h3>Sucette</h3>
        <span>A partir de <strong id="price-sucette">25 €</strong> </span>
      </div>
    </div>
    <div class="grid-item-product">
      <img src="./images/cannes.jpg" alt="bonbon cannes">
      <div class="card-content">
        <button class="control-price">
          <i class="fa-sharp fa-solid fa-pen"></i>
        </button>
        <h3>Cannes</h3>
        <span>A partir de <strong id="price-cannes">25€</strong> </span>
      </div>
    </div>
    <div class="grid-item-product">
      <img src="./images/ourson.jpg" alt="bonbon oursson">
      <div class="card-content">
        <button class="control-price">
          <i class="fa-sharp fa-solid fa-pen"></i>
        </button>
        <h3>Ourson</h3>
        <span>A partir de <strong id="price-ourson">25€</strong> </span>
      </div>
    </div>
  </div>
</article>

<!-- end snippet -->
英文:

document.querySelector will return the first matched element, you might be looking for querySelectorAll with a way to identify each individual "price reduction" element.

So instead you might select all your buttons with:

let promoFormOpenButtons = document.querySelectorAll(&quot;.control-price&quot;);

Then add your event listeners to every button:

promoFormOpenButtons.forEach(btn =&gt; {
btn.addEventListener(&#39;click&#39;, (e) =&gt; {
// do something
});

Full example:

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

<!-- language: lang-js -->

let promoFormOpenButtons = document.querySelectorAll(&quot;.control-price&quot;);
promoFormOpenButtons.forEach(btn =&gt; {
btn.addEventListener(&#39;click&#39;, (e) =&gt; {
console.log(e.target.parentElement.querySelector(&#39;h3&#39;).textContent);
});
});

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

&lt;article class=&quot;grid-product&quot;&gt;
&lt;div class=&quot;grid-items-product&quot;&gt;
&lt;div class=&quot;grid-item-product&quot;&gt;
&lt;img src=&quot;./images/sucette.jpg&quot; alt=&quot;sucette&quot;&gt;
&lt;div class=&quot;card-content&quot;&gt;
&lt;button class=&quot;control-price&quot;&gt;
&lt;i class=&quot;fa-sharp fa-solid fa-pen&quot;&gt;&lt;/i&gt;
&lt;/button&gt;
&lt;h3&gt;Sucette&lt;/h3&gt;
&lt;span&gt;A partir de &lt;strong id=&quot;price-sucette&quot;&gt;25 €&lt;/strong&gt; &lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;grid-item-product&quot;&gt;
&lt;img src=&quot;./images/cannes.jpg&quot; alt=&quot;bonbon cannes&quot;&gt;
&lt;div class=&quot;card-content&quot;&gt;
&lt;button class=&quot;control-price&quot;&gt;
&lt;i class=&quot;fa-sharp fa-solid fa-pen&quot;&gt;&lt;/i&gt;
&lt;/button&gt;
&lt;h3&gt;Cannes&lt;/h3&gt;
&lt;span&gt;A partir de &lt;strong id=&quot;price-cannes&quot;&gt;25€&lt;/strong&gt; &lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;grid-item-product&quot;&gt;
&lt;img src=&quot;./images/ourson.jpg&quot; alt=&quot;bonbon oursson&quot;&gt;
&lt;div class=&quot;card-content&quot;&gt;
&lt;button class=&quot;control-price&quot;&gt;
&lt;i class=&quot;fa-sharp fa-solid fa-pen&quot;&gt;&lt;/i&gt;
&lt;/button&gt;
&lt;h3&gt;Ourson&lt;/h3&gt;
&lt;span&gt;A partir de &lt;strong id=&quot;price-ourson&quot;&gt;25€&lt;/strong&gt; &lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/article&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月12日 19:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712772.html
匿名

发表评论

匿名网友

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

确定