admin管理员组文章数量:1406308
If I have a program that is adding and taking away html elements dynamically and each one of these elements needs to have an event listener is there a way to add the event listener to a variable that's easier than doing it one by one? I wanted to use an array and have each index of the array to have a event listener attached to it but that doesn't seem to work.
var1 = document.getElementById('name');
var1.addEventListener("mousedown", doMouseDown, false);
This works fine but I have about 100 elements so I was hoping there is an easier way than creating 100 individual variables. I there is an easier way to do this I would love any suggestions. :)
If I have a program that is adding and taking away html elements dynamically and each one of these elements needs to have an event listener is there a way to add the event listener to a variable that's easier than doing it one by one? I wanted to use an array and have each index of the array to have a event listener attached to it but that doesn't seem to work.
var1 = document.getElementById('name');
var1.addEventListener("mousedown", doMouseDown, false);
This works fine but I have about 100 elements so I was hoping there is an easier way than creating 100 individual variables. I there is an easier way to do this I would love any suggestions. :)
Share Improve this question edited Jan 15, 2022 at 20:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 16, 2012 at 17:02 user1271792user1271792 412 silver badges4 bronze badges3 Answers
Reset to default 5Code repetition can usually be avoided by using loops. In this case, create a list (array) of all IDs, and loop through it:
var names = ['name1', 'name2', 'etc'];
for (var i=0; i<names.length; i++) {
document.getElementById(name[i]).addEventListener("mousedown",
doMouseDown, false);
}
If all of your elements have no descendants, you can also bind one event to the mon parent, and check the event.target
property:
var names = '|name1|name2|';
document.addEventListener('mousedown', function(event) {
// Use String.indexOf, because old IE browsers do not support Array.indexOf
if (names.indexOf( '|' + event.target.id + '|') !== -1) doMouseDown(event);
}, false);
If all these elements are children of a container element, you can add a single event listener to the parent element to capture all these events.
See http://www.quirksmode/js/events_order.html for more info on the way javascript events are passed up and own the DOM tree.
you can organize your elements by giving them the same name and then putting them into array using getElementsByName. Afterwards you could loop through the array and add listeners to each element.
本文标签: htmlAdding multiple event listeners dynamically in JavaScriptStack Overflow
版权声明:本文标题:html - Adding multiple event listeners dynamically in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745006338a2637286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论