[英]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.