gpt4 book ai didi

jQuery .on 效率

转载 作者:行者123 更新时间:2023-12-01 07:14:32 27 4
gpt4 key购买 nike

第一个 .on 函数比第二个更有效吗?

$( "div.container" ).on( "click", "p", function(){
});

$( "body" ).on( "click", "p", function(){
});

问候

最佳答案

来自 jQuery 文档本身关于 jQuery: .on() :

jQuery can process simple selectors of the form tag#id.class very quickly when they are used to filter delegated events. So, "#myForm", "a.external", and "button" are all fast selectors. Delegated events that use more complex selectors, particularly hierarchical ones, can be several times slower--although they are still fast enough for most applications. Hierarchical selectors can often be avoided simply by attaching the handler to a more appropriate point in the document. For example, instead of $( "body" ).on( "click", "#commentForm .addNew", addComment ) use $( "#commentForm" ).on( "click", ".addNew", addComment ).

关于jQuery .on 效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com