简体   繁体   English

jquery 多类选择器

[英]jquery selector for multiple classes

I have elements in my DOM with class="LiveVal:variablepart" and i would like to write a JQuery selector that works even if the elements have other classes on tom of the above.我的 DOM 中有带有 class="LiveVal:variablepart" 的元素,我想编写一个 JQuery 选择器,即使这些元素在上面的 tom 上有其他类也可以工作。 Eg.例如。 class="header LiveVal:varablepart" or class="LiveVal:varablepart header". class="header LiveVal:varablepart" 或 class="LiveVal:varablepart header"。

It works fro me if LiveVal is the first class with:如果 LiveVal 是第一个 class ,它对我有用:

$('[class^=LiveVal:]').each(function ( intIndex ) { somefunction });

but obviously not if another class is before LiveVal.但如果另一个 class 在 LiveVal 之前,显然不会。

In the function I need to extract the variable part.在 function 我需要提取可变部分。 I planned to do like this:我打算这样做:

theclass = $( this ).attr('class');
varpart = theclass.replace('\bLiveVal:(.+?)[\s]', '$1');

..but alas, it doesn't match. ..但唉,它不匹配。 I've tested the regex on http://gskinner.com/RegExr/ where it seems to work, but it doesn't in javascript??我已经在http://gskinner.com/RegExr/上测试了它似乎可以工作的正则表达式,但它在 javascript 中不起作用?

Any help would be greatly appreciated.任何帮助将不胜感激。

This will check if a class name contains 'LiveVal:'这将检查 class 名称是否包含“LiveVal:”

$('[class*=LiveVal:]').each(function ( intIndex ) { somefunction });

EDIT did not realise you had that requirement (although a good one). EDIT没有意识到你有这个要求(虽然是一个很好的要求)。 You can do this instead: $('[class^="LiveVal:"], [class*=" LiveVal:"]')你可以这样做: $('[class^="LiveVal:"], [class*=" LiveVal:"]')

Here is a fiddle: http://jsfiddle.net/wY8Mh/这是一个小提琴: http://jsfiddle.net/wY8Mh/

It might be somewhat faster to do this with an explicit filter:使用显式过滤器执行此操作可能会更快一些:

 $("*").filter(function() { return /\bLiveVal:/.test(this.className); }).something();

It depends on whether the native "querySelectorAll" does the work, and does it quickly.这取决于本机“querySelectorAll”是否可以完成工作,并且可以快速完成。 This also would avoid the "FooLiveVal" problem.这也可以避免“FooLiveVal”问题。

It's worth noting that in an HTML5 world, it might be better to use a "data-LiveVal" attribute to store that "variable part" information on your elements.值得注意的是,在 HTML5 世界中,使用“data-LiveVal”属性将“变量部分”信息存储在元素上可能会更好。 Then you could just say:然后你可以说:

$('[data-LiveVal]').something();

In the HTML, it'd look like this:在 HTML 中,它看起来像这样:

<div class='whatever' data-LiveVal='variable part'>

Since version 1.5, jQuery will fetch stuff in a "data-foo" attribute when you pass the tail of the attribute (the part after "data-") to the ".data()" method:从 1.5 版开始,当您将属性的尾部(“data-”之后的部分)传递给“.data()”方法时,jQuery 将在“data-foo”属性中获取内容:

  var variablePart = $(this).data('LiveVal');

The ".data()" method will not , however, update the "data-foo" property when you store a new "variable part".但是,当您存储新的“变量部分”时,“.data()”方法不会更新“data-foo”属性。

edit — if you want the value that's stuffed into the class after your property name prefix ("LivaVal:"), you can extract it like this:编辑——如果你想要在你的属性名称前缀(“LivaVal:”)之后填充到 class 中的,你可以像这样提取它:

  var rLiveVal = /\bLiveVal:(\S*)\b/;
  $('*').filter(function() { return rLiveVal.test(this.className); }).each(function() {
    var variablePart = rLiveVal.exec(this.className)[1];
    //
    // ... do something ...
    //
  });

(or some variation on that theme). (或该主题的一些变体)。

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

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