简体   繁体   English

将事件侦听器添加到HTML元素集合

[英]Add Event Listener to Collection of HTML Elements

I know that getElementsByTagName and getElementsByClassName need an index identifier in order for the objects to be bound to an event listener. 我知道getElementsByTagNamegetElementsByClassName需要一个索引标识符,以便将对象绑定到事件监听器。

So the question is, how do I add an event listener to a collection of HTML elements found using getElementsByTagName or getElementsByClassName ? 所以问题是, 如何将事件侦听器添加到使用 getElementsByTagName getElementsByClassName 找到的HTML元素集合中

<input class="inputs" type="submit" value="Hello" />
<input class="inputs" type="submit" value="World" />

var inputElem = document.getElementsByTagName('input');
inputElem.addEventListener('click', function(){
    alert(this.value);
}, false);

I know how to do this in jQuery, but I want to know how to do it with pure JS. 我知道如何在jQuery中执行此操作,但我想知道如何使用纯JS执行此操作。

It's pretty simple like @Rutwick Gangurde said. 就像@Rutwick Gangurde所说的那样非常简单。 Once you get the elements you just need to loop through and attach the event. 获得元素后,您只需循环并附加事件即可。

var inputElem = document.getElementsByTagName('input');

for(var i = 0; i < inputElem.length; i++) {

    inputElem[i].addEventListener('click', function(){
        alert(this.value);
    }, false);
}

Here it is in a fiddle: http://jsfiddle.net/wm7p0a77/ 这是一个小提琴: http//jsfiddle.net/wm7p0a77/

Adding eventlistener to each of them is not advisable. 不建议将eventlistener添加到每个中。 May be this can help: 可能这可以帮助:

http://jsfiddle.net/b8gotLL6/ http://jsfiddle.net/b8gotLL6/

document.getElementById('parent').addEventListener('click', function(e){
alert(e.target.value);
})

And if you only want to do using getElementsByTagName or getElementsByClassName , then i guess you need to iterate for the array returned. 如果你只想使用getElementsByTagNamegetElementsByClassName ,那么我猜你需要迭代返回的数组。

Try querySelectorAll method. 尝试querySelectorAll方法。

var inputElem = document.querySelectorAll('input');

Which returns a NodeList and you can loop through the array to add the event listeners. 返回NodeList ,您可以遍历数组以添加事件侦听器。

you can try like this:first get all the element of the particular type the loop through it. 你可以尝试这样:首先通过它获取特定类型的所有元素循环。

 var elems = document.getElementsByClassName('inputs'); for(var i = 0; i < elems.length; i++) { elems[i].addEventListener('click', function(){ alert(this.value); }, false); } 
 <input class="inputs" type="submit" value="Hello" /> <input class="inputs" type="submit" value="World" /> 

You can use the class too as a selector. 您也可以将该类用作选择器。

var elems = document.getElementsByClassName('inputs');

Then loop over these to attach event handlers. 然后遍历这些以附加事件处理程序。

var i=0, inputElem = document.getElementsByTagName('input'), len = inputElem.length;    
while(i < len){
  inputElem[i].addEventListener('click', function(){
      alert(this.value);
  }, false);
 i++;
}

You can use delegates to implement this. 您可以使用委托来实现此目的。 Get hold of a parent element to these inputs and add an event listener to that parent element. 获取这些输入的父元素,并向该父元素添加事件侦听器。 This way you will be just attaching one event and make use of event bubbling to accomplish your task. 这样您就可以只附加一个事件并利用事件冒泡来完成任务。 In that event listener, you might have to check the target of the event and if that target equals the input element, you can run your logic inside this condition. 在该事件侦听器中,您可能必须检查事件的目标,如果该目标等于input元素,则可以在此条件下运行逻辑。

You can do something like this in your event handler function. 您可以在事件处理函数中执行类似的操作。

// ...
// get event and source element e = e || window.event;
src = e.target || e.srcElement;
if (src.nodeName.toLowerCase() !== "input") {
    return; 
}
// ...

First, use getElementsByClassName, instead of getElementsByTagName. 首先,使用getElementsByClassName而不是getElementsByTagName。 Then Loop through them, adding the event listener like this: 然后遍历它们,添加如下事件监听器:

var inputElem = document.getElementsByClassName('inputs');
var i;
for (i = 0; i < inputElem .length; i++) {
    inputElem [i].addEventListener('click', (function(i) {
        return function() {
           alert(this.value);
        };
    })(i), false);
}

Here it is on jsfiddle 这是jsfiddle

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

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