admin管理员组文章数量:1291519
If, for example, I have series of event listeners created like this:
var els = document.getElementyById('myList').getElementsByTagName('li');
for (i=0;i<els.length;i++) {
els[i].addEventListener(eventType, function(e){ /* do stuff */ }, true);
}
First off, am I mitting some kind of heresy like this? I mean, is there a faaar simpler way of doing this other than an event for each <li>
element?
In either case, the main question is: what happens to those event listeners if the <li>
s are removed/replaced? What would happen if I did this:
document.getElementyById('myList').innerHTML = 'Hello World!';
Do the listeners stay 'suspended' thus slowing down the browser (assume I have a lot of <li>
s), or are they automatically removed? Is it even an issue?
If, for example, I have series of event listeners created like this:
var els = document.getElementyById('myList').getElementsByTagName('li');
for (i=0;i<els.length;i++) {
els[i].addEventListener(eventType, function(e){ /* do stuff */ }, true);
}
First off, am I mitting some kind of heresy like this? I mean, is there a faaar simpler way of doing this other than an event for each <li>
element?
In either case, the main question is: what happens to those event listeners if the <li>
s are removed/replaced? What would happen if I did this:
document.getElementyById('myList').innerHTML = 'Hello World!';
Do the listeners stay 'suspended' thus slowing down the browser (assume I have a lot of <li>
s), or are they automatically removed? Is it even an issue?
- 3 they are also garbage collected – Arun P Johny Commented Jul 10, 2013 at 13:05
- @ArunPJohny ...meaning? – Sean Bone Commented Jul 10, 2013 at 13:06
-
5
"is there a faaar simpler way of doing this other than an event for each <li> element?" - Using a delegated handler attached to the containing
ul
element makes sense here. Not necessarily simpler, but only slightly more plicated and more efficient - or maybe it is simpler if you are dynamically addingli
elements. And it pletely avoids the issue you are worried about. – nnnnnn Commented Jul 10, 2013 at 13:07 - 2 Meaning they are removed from memory. Also, if every one of your events does the same thing, bind them to the enpassing <ul> - click events will bubble up from the lis to the ul. – Maxim Kumpan Commented Jul 10, 2013 at 13:07
- If the same code is to be executed by the event, you'd want to at least create the function first and then reference it to the eventListener so you don't have multiple identical anonymous functions. – Salketer Commented Jul 10, 2013 at 14:52
1 Answer
Reset to default 13Event handlers assigned to destroyed elements are marked for garbage collection. Meaning they are removed from memory. Also, if every one of your events does the same thing, bind them to the enpassing <ul>
- click events will bubble up from the li
s to the ul
.
Also,
"is there a faaar simpler way of doing this other than an event for each
<li>
element?"Using a delegated handler attached to the containing ul element makes sense here. Not necessarily simpler, but only slightly more plicated and more efficient - or maybe it is simpler if you are dynamically adding li elements. And it pletely avoids the issue you are worried about.
(Courtesy of @nnnnnn)
Converted ment to answer as it seems to have helped answer the question.
本文标签: javascriptWhat happens to event listeners when element is removedStack Overflow
版权声明:本文标题:javascript - What happens to event listeners when element is removed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741531484a2383780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论