admin管理员组文章数量:1406937
I have a <ul>
that has many children (close to 3000 items) and some of the <li>
s have many levels. I've attached an event listener on 'click' (I'm using jQuery) which I use to toggle the visibility of the children of an <li>
.
I'm wondering how having so many event listeners impacts performance. (There are at the very least 1000!). Is this a big issue for performance?
I'm not really seeing much of an issue performance wise with newer web browsers, but IE8 seems to be very slow. Is it madly irresponsible to just whack an event listener on everything?!
I have a <ul>
that has many children (close to 3000 items) and some of the <li>
s have many levels. I've attached an event listener on 'click' (I'm using jQuery) which I use to toggle the visibility of the children of an <li>
.
I'm wondering how having so many event listeners impacts performance. (There are at the very least 1000!). Is this a big issue for performance?
I'm not really seeing much of an issue performance wise with newer web browsers, but IE8 seems to be very slow. Is it madly irresponsible to just whack an event listener on everything?!
Share Improve this question asked Jul 24, 2013 at 18:02 Richard SweeneyRichard Sweeney 7801 gold badge14 silver badges26 bronze badges1 Answer
Reset to default 9The answer is a big whooping YES. Yes, it will affect performance. Event delegation was built for this exact thing. Instead of binding your handler to every single li
, bind it to its parent, the ul
. This way the ul
will delegate the click
event to li
.
$("ul").on("click", "li", function () {
//your code
});
本文标签: jqueryJavaScript event listener performance for hundreds of DOM elementsStack Overflow
版权声明:本文标题:jquery - JavaScript event listener performance for hundreds of DOM elements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744967646a2635045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论