简体   繁体   English

选择一组元素的最有效方法?

[英]Most efficient way to select a set of elements?

I've always used class names to select sets of related elements. 我一直使用类名来选择相关元素集。 eg 例如

<input type="checkbox" value="1" class="checkbox_set_1">
<input type="checkbox" value="2" class="checkbox_set_1">
<input type="checkbox" value="3" class="checkbox_set_1">

$('.checkbox_set_1').filter(':checked') ...

I do this because I know jQuery can delegate to document.getElementsByClassName which should be pretty fast. 我这样做是因为我知道jQuery可以委托给document.getElementsByClassName ,这应该很快。 However, adding classes to all the elements I want to select but not style seems kind of dirty. 但是,将类添加到我要选择的所有元素中,而不是样式,似乎有点脏。 Isn't there some overhead when the browser has to check checkbox_set_1 against its stylesheet to determine if my checkboxes need styling? 当浏览器必须对照其样式表检查checkbox_set_1以确定我的复选框是否需要样式时,会不会有一些开销? Plus, there's some risk of accidental styling if I haven't named my classes nicely. 另外,如果我没有很好地命名自己的班级,那还会有意外样式化的风险。

Is there a better way to select elements that doesn't rely on an attribute meant for styling, without giving up the performance benefits? 有没有更好的方法来选择不依赖于样式属性的元素,而又不损失性能? Or more specifically, is there an attribute other than class (used for styling) and id (limited to a single element) that the browser will optimize queries for? 或更具体地说,浏览器是否会针对其优化查询的class (用于样式)和id (仅限于单个元素)以外的属性?

There are many other attributes to pick from, including data-* attributes, but I don't think the browser optimizes lookups on anything other than id and class , does it? 还有许多其他属性可供选择,包括data-*属性,但我认为浏览器除了对idclass进行优化之外,不会对其他任何内容进行优化,对吗?

Isn't there some overhead when the browser has to check checkbox_set_1 against its stylesheet to determine if my checkboxes need styling? 当浏览器必须对照其样式表检查checkbox_set_1以确定我的复选框是否需要样式时,会不会有一些开销?

Styling isn't determined that way. 样式不是以这种方式确定的。 The browser doesn't take each attributes of an element and look for rules that apply, instead it looks through the rules once and determine which ones applies to the element. 浏览器不采用元素的每个属性,而是寻找适用的规则,而是浏览规则一次,并确定哪些规则适用于元素。

If you are concerned with adding classes to a lot of elements, you can use selectors that make better use of the document structure, for example setting a class on the element containing the checkboxes and use something like $('.CheckboxContainer :checked') . 如果您想将类添加到许多元素中,则可以使用可以更好地利用文档结构的选择器,例如,在包含复选框的元素上设置类,并使用类似$('.CheckboxContainer :checked')

That might not be quite as efficient as setting classes on every element that you want to target, but in most cases the difference is far from noticable. 这可能不如在要定位的每个元素上设置类那么有效,但是在大多数情况下,区别并不明显。 You shouldn't bother too much about efficient selectors until it's an actual problem. 除非这是一个实际问题,否则您不应该为高效的选择器而烦恼。 After all, you are using jQuery because it's convenient, not to get the best performance. 毕竟,您使用jQuery是因为它很方便,而不是获得最佳性能。

Yes, it behaves analouge to the "Marker-Interface in java" and you will find the anti-pattern-description of Tom Butler . 是的,它的行为类似于“ java中的标记器接口”,并且您会发现Tom Butler反模式描述

If you have a form around, you could use elements (but you must filter other elements) this is faster than calling a method like "getElementsByClassName". 如果周围有表单,则可以使用元素 (但必须过滤其他元素),这比调用“ getElementsByClassName”之类的方法要快。

Example

 var lst = jQuery(document.foo.elements); document.write(lst.length); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form name="foo"> <input type="checkbox" value="1" /> <input type="checkbox" value="2" /> <input type="checkbox" value="3" /> <input type="checkbox" value="4" form="bar" /> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM