简体   繁体   English

强大的选择器来查找类的所有元素

[英]Robust selector to find all elements of class

How can I get all elements who have a class that begins with vd- ? 如何获得所有以vd-开头的类的元素? My code below works for most cases except for the following: 我的以下代码适用于大多数情况,但以下情况除外:

class="foo vd-bar"

It works for these cases: 它适用于以下情况:

class="vd-bar foo"

Here is my selector, will it work if the HTML markup uses single quotation marks? 这是我的选择器,如果HTML标记使用单引号可以使用吗? For eg; 例如 class='vd-foo' ? class='vd-foo'

$('[class^="vd-"]', myElement).each(function(index, ele) {


});

Unfortunately, CSS Level 3 attribute selectors are very limited (and Level 4 in draft are not much better). 不幸的是,CSS 3级属性选择器非常有限( 草稿中的4级并没有更好)。

You will have to use a combination of infix-match and prefix-match attribute selectors to achieve a robust selection of all elements with a class that begins with vd- : 您将必须使用infix-match和prefix-match属性选择器的组合,以vd-开头的类对所有元素进行可靠的选择:

[class*=" vd-"], [class^="vd-"] {
    ...
}

The first one will select elements with value of attribute class containing <space>vd- (cases like foo vd-... ) and the second one will patch the corner case of vd- class being the first one (cases like vd-bar foo ). 第一个将选择属性class值包含<space>vd- (例如foo vd-... ),第二个将修补vd- class作为第一个的vd-情况(例如vd-bar foo )。

Performance 性能

One might be tempted to use only infix (substring) selector, on account of assumed performance penalty incurred by usage of double attribute selectors (infix and prefix match), knowingly sacrificing the accuracy and accepting mismatches like yrivd-button for vd-button . 考虑到使用双属性选择器(中缀和前缀匹配)会导致性能下降,故意牺牲准确性并接受不匹配(例如yrivd-button用作vd-button ),可能会倾向于使用infix (子字符串)选择器。

However, measurements show there is no statistically significant difference in performance between only infix match selector and the combined infix+prefix match selector (compare substringMatch and classPrefixSelector on jsperf.com test). 但是, 测量结果显示仅在infix匹配选择器组合的infix + prefix匹配选择器之间(在jsperf.com测试中比较substringMatchclassPrefixSelector ),性能没有统计学上的显着差异

The combined selector is only 2% to 7% slower (depending on browser) than a single attribute value match selector. 组合的选择器仅比单个属性值匹配选择器慢2%至7% (取决于浏览器)。

If someone is concerned about that, she shouldn't use attribute selectors at all, but only class selectors .vd-name , which are 2 to 3 times faster than any of the attribute selectors (including the simplest one [attr] , which only tests for attribute existence). 如果有人对此感到担忧,那么她根本就不应该使用属性选择器,而应该只使用类选择器.vd-name比任何一个属性选择器(包括最简单的[attr] 快2至3倍测试属性是否存在)。

You need to use the contains selector ( * ), not the start with selector ( ^ ). 您需要使用包含选择器( * ),而不是使用选择器( ^ )开头。 Other than that, it doesn't really matter whether you use single or double quotes as long as you match them correctly. 除此之外,只要您正确匹配单引号或双引号就没有关系。 Here is an example: 这是一个例子:

 $("[class*='vd-']").css("border", "1px solid #f00"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="foo vd-bar">first div</div> <div class='vd-bar foo'>second div</div> <div class='fvd-bar foo'>third div</div> 

Note: As you can see, the code above will select everything that contains "vd-". 注意:如您所见,上面的代码将选择包含“ vd-”的所有内容。 That should be fine for most cases because usually class prefixes don't occur in the middle of the class name. 在大多数情况下,这应该没问题,因为通常在类名的中间不会出现类前缀。 If they do, that's considered a bad design. 如果他们这样做,那被认为是不好的设计。 If you want to only select elements with classes that start with "vd-", that cannot be done in a simple selector, so you'll have to use two selectors like randomir's answer below. 如果您只想选择以“ vd-”开头的类的元素,则无法在简单的选择器中完成,因此您必须使用两个选择器,例如下面的randomir答案。 That has a performance cost in big documents, so it is better to avoid it unless it is necessary. 在大型文档中这会降低性能,因此最好避免这样做,除非有必要。 Here is a jQuery example: 这是一个jQuery示例:

 $("[class*=' vd-'],[class^='vd-']").css("border", "1px solid #f00"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="foo vd-bar">first div</div> <div class='vd-bar foo'>second div</div> <div class='fvd-bar foo'>third div</div> 

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

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