简体   繁体   English

如何在addEventListener中捕获两个相同的ID但不同的类

[英]How to capture two same id but different class in addEventListener

first of all, thank you for your time to read this question, and two things, I'm using ES5 and I don't use jQuery. 首先,感谢您抽出宝贵的时间阅读此问题,还有两件事,我正在使用ES5,而我没有使用jQuery。

Right now I'm struggling a lot to figure what's the correct solution for the addEventListener, because for some reason it does not trigger for the second button which is only for the mobile screen dimensions, the problem is that the second button have the same id but different class, for example this: 现在,我在努力寻找addEventListener的正确解决方案是什么,因为出于某种原因它不会触发仅适用于移动屏幕尺寸的第二个按钮,所以问题在于第二个按钮具有相同的ID但类不同,例如:

 <div class="product-bg-container product-general-info variation-info">
  <input type="hidden" name="sku" value="Something-15892290" id="selected-option">

  {/* Desktop screen button */}
  <button id="buy-now" class="btn btn-lg hidden-sm-down btn-primary">
    Add to Cart
  </button>

  {/* Mobile screen button */}
  <button id="buy-now" class="btn btn-lg hidden-md-up btn-primary"> 
    Add to Cart 
  </button>
 </div>

Where I am trying to trigger the second button but it does not where I don't understand why it does, if the id is the same, should not matter, so I'm trying to figure how to trigger from the first button if it's clicked and also with the second if it's clicked, but I'm out of ideas... 我尝试在何处触发第二个按钮,但在哪里我不明白为什么这样做,如果id相同,就无关紧要,所以我试图弄清楚如何从第一个按钮触发单击,如果单击也单击第二个,但是我没有主意...

var button = document.getElementById('buy-now');

if (!button) {
    return;
  }

button.addEventListener('click', function trackAddToCart() { 
  // more code for the event
}

I thought an idea to capture the attribute of the button, but it works in the first button but not for the second one: 我以为可以捕获按钮的属性,但是它在第一个按钮中有效,但在第二个按钮中无效:

var button = document.getElementById('buy-now');
var att = button.getAttribute('class');

  button.addEventListener('click', function() {
        console.log('class ' + att); //shows: class:    btn btn-lg hidden-sm-down btn-primary
        console.log('button class? '+ button); //shows:  button element: [object HTMLButtonElement]
      });

But when I click the second button... does not trigger or happening nothing, not sure why... and I can't change the id value (which it should be easy but I can't "company standard") 但是,当我单击第二个按钮时...不会触发或什么也没有发生,不知道为什么...并且我不能更改id值(这应该很容易,但是我不能“符合公司标准”)

Can anyone help me to have an idea how to capture and trigger the event for the second button ?? 谁能帮助我了解如何捕获和触发第二个按钮的事件?

The attribute id must be unique in a document. 属性ID在文档中必须唯一。 You can use attributeStartsWith selector or class with querySelectorAll() . 您可以使用attributeStartsWith选择器或带有querySelectorAll() Then loop through all the button to attach the event ( click ) individually: 然后循环浏览所有按钮以单独附加事件( click ):

 //var button = document.querySelectorAll('.btn.btn-primary'); var button = document.querySelectorAll('[id^=buy-now]'); button.forEach(function(btn){ btn.addEventListener('click', function() { console.log('class ' + this.classList); console.log('button class? '+ this.id); }); }); 
 <div class="product-bg-container product-general-info variation-info"> <input type="hidden" name="sku" value="Something-15892290" id="selected-option"> <button id="buy-now" class="btn btn-lg hidden-sm-down btn-primary"> Add to Cart </button> <button id="buy-now2" class="btn btn-lg hidden-md-up btn-primary"> Add to Cart </button> </div> 

nextElementSibling seems working in this case. 在这种情况下, nextElementSibling似乎起作用。

 var btn1 = document.getElementById("btn"); var btn2 = btn1.nextElementSibling; btn1.addEventListener("click",function(e){ console.log("btn1"); }); btn2.addEventListener("click",function(e){ console.log("btn2"); }); 
 <div> <button id="btn" class="btn1">butotn 1</button> <button id="btn" class="btn2">butotn 2</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM