gpt4 book ai didi

jQuery 点击多个选择器

转载 作者:行者123 更新时间:2023-12-01 03:18:27 24 4
gpt4 key购买 nike

我想知道哪种方法更好以及为什么:A)单击一个文档并使用多个选择器,B)单击多个文档。

A 的选择器也有限制吗?谢谢。

A)

$(document).on('click', '#a, #b, #c, #d, #e', function(e){

});

对比

B)

$(document).on('click', '#a', function(e){

});


$(document).on('click', '#b', function(e){

});

$(document).on('click', '#c', function(e){

});

$(document).on('click', '#d', function(e){

});

$(document).on('click', '#e', function(e){

});

最佳答案

选项A称为多选择器方法http://api.jquery.com/multiple-selector/ - 选择所有指定选择器的组合结果

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order. An alternative to this combinator is the .add() method.

性能(选择器) = id、类、标签、伪、属性选择器 ==> http://jsperf.com/id-vs-class-vs-tag-selectors/2

我看到的多个选择器的优点:

推荐

如果所有的 id 都做同样的事情,请考虑使用 class 属性而不是这么多 id

像这样

$(function() {
$(".classname").click(function() {

});
});​

关于jQuery 点击多个选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12946454/

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