简体   繁体   English

如何在具有特定类的所有dom元素上添加EventListener?

[英]How to add an EventListener on all the dom element that have a specific class?

I have some span in my DOM that have the class="foo" and all the spans are created dynamically. 我的DOM中有一些具有class =“ foo”的跨度,并且所有跨度都是动态创建的。

With TypeScript i want to add an onClick Event to all of them after their creation. 我想使用TypeScript在它们创建后向所有它们添加一个onClick事件。

I have something like this : 我有这样的事情:

var foos = document.body.querySelectorAll(".foo");
foos.forEach(function (item) {            
    item.addEventListener("click", () => { 
        console.log(item); 
    });
});

But i can't do forEach on a NodeListOf. 但是我不能在NodeListOf上做forEach。

foos = Array.from(foos) does not work because : 'from' doesn't exist on type 'ArrayConstructor'. foos = Array.from(foos)不起作用,因为:'from'在'ArrayConstructor'类型上不存在。

Since you tagged your question with jquery , here's the jquery way: 由于您使用jquery标记了问题,因此这是jquery方式:

$(".foo").click( function() { 
   // Do something here
}); 

Use simple for loop for this. 为此使用简单的for循环。 You can get elements for each index in the foos array and assign click event on them. 您可以在foos数组中获取每个索引的元素,并为其分配click事件。 This is because querySelectorAll returns a NodeList which is indexed like an array, but not an Array so you can't call the array methods on it as it is not a real array. 这是因为querySelectorAll返回的NodeList的索引类似于数组,而不是数组,因此您不能在其上调用数组方法,因为它不是真正的数组。

var foos = document.body.querySelectorAll(".foo"));
for(var i=0; i<foos.length; i++){
    foos[i].addEventListener("click", () => { 
        console.log(item); 
    });
});

I made three different approaches that you can use, they are very simple, please open each code snippet to see it working. 我提供了三种可以使用的不同方法,它们非常简单,请打开每个代码段以查看其工作原理。

1 - This Example shows how to add the listener AFTER the creation of elements. 1-此示例显示如何在元素创建添加侦听器。

 for (var i = 0; i < 4; i++){ var a = document.createElement('input'); a.type = 'button'; a.value = 'click me ' + i; a.className = 'foo'; document.body.append(a); } //JQUERY WAY $('.foo').on('click', function(){ console.log(this.value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

2 - This Example shows how to add the listener BEFORE the creation of elements. 2-此示例显示了如何元素创建之前添加侦听器。

 //JQUERY WAY $(document).on('click', '.foo', function(){ console.log(this.value); }); for (var i = 0; i < 4; i++){ var a = document.createElement('input'); a.type = 'button'; a.value = 'click me ' + i; a.className = 'foo'; document.body.append(a); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

3 - And finally, if don't want jQuery, here's a Vanilla JS example: 3-最后,如果不想使用jQuery,下面是Vanilla JS示例:

 for (var i = 0; i < 4; i++){ var a = document.createElement('input'); a.type = 'button'; a.value = 'click me ' + i; a.className = 'foo'; document.body.append(a); } //VANILLA JS WAY var myElems = document.querySelectorAll('.foo'); myElems.forEach(function(elem){ elem.onclick = function(){ console.log(elem.value); } }); 

暂无
暂无

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

相关问题 如何在不知道特定元素的类或ID的情况下将其定位为目标,但确实具有该元素的所有DOM详细信息 - How to target a specific element without knowing it's class or id, but do have all DOM details of the element 在dom中查找所有特定元素,并在下一个元素上添加类 - Find all specific element in dom and add class on next element 如何使用 EventListener 将 css class 添加到元素 - How to add css class to an element with EventListener 如何将eventlistener添加到使用dom随机生成的元素? - how to add eventlistener to elements that have been generated randomly using dom? 如何添加EventListener来侦听除特定元素之外的每次单击窗口的事件 - How to add an EventListener that listens every click on window besides a specific element 如果任何孩子在 jquery 中有特定的类,如何将类添加到元素? - how to add class to element if any of children have specific class in jquery? 如何遍历具有特定类别的下一个dom元素? - How to traverse to next dom element with specific class? 如何在 DOM 元素中添加 javascript Class 的实例 - How to add instance of javascript Class in a DOM element 如何检查DOM中div的所有类名是否都有一个带有vanilla Javascript的特定字符串? - How to check if all class names of divs in the DOM have a specific string with vanilla Javascript? 如何将事件监听器添加到选择元素 - How do you add an eventlistener to a select element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM