简体   繁体   English

如何在jQuery和CSS中使用*取得类?

[英]How to get class with * in jQuery and CSS?

I have many elements: 我有很多要素:

<span class="test"></span>
<span class="aaa"></span>
<span class="test-one"></span>
<span class="test-two"></span>
<span class="aaa-one"></span>
<span class="test-two"></span>

How can i get with one select all span with name test*? 如何使用全选名称test *选择全部范围? I can: 我可以:

$('.test, .test-one, .test-two')

but maybe is possible to get this with regex? 但是也许可以用正则表达式来做到这一点?

$('.test*')

?

in css: 在CSS中:

 .test, .test-one, .test-two

 .test*

You're abusing classes, and are looking for multiple classes instead: 您正在滥用课程,而是在寻找多个课程:

<span class="test"></span>
<span class="aaa"></span>
<span class="test one"></span>
<span class="test two"></span>
<span class="aaa one"></span>
<span class="test two"></span>

Now, $('.one') will correctly return the 3rd and 5th element, and $('.test') will return all elements except the 2nd and 5th. 现在, $('.one')将正确返回第3和第5个元素,而$('.test')将返回除第2和第5个元素之外的所有元素。

Note that you can also use $('.two.test') to get the 4th and 6th element. 请注意,您还可以使用$('.two.test')获取第4个和第6个元素。

Use the starts-with selector: 使用开始方式选择器:

$('span[class^="test"]').

http://api.jquery.com/attribute-starts-with-selector/ http://api.jquery.com/attribute-starts-with-selector/

It's not clear from the small example whether there's a pattern to the -one and -two class names. 从一个小的示例尚不清楚-one-two类名称是否存在模式。

If there is a pattern, and your idea is to have alternating classes (eg odd and even rows?), then you might want to consider using the nth-child() selector. 如果有一种模式,而您的想法是要有交替的类(例如,奇数行和偶数行?),那么您可能要考虑使用nth-child()选择器。 This will allow you to select the relevant elements without needing to reference any class names at all. 这将使您可以选择相关元素,而无需引用任何类名。

In this case, you could do something like this: 在这种情况下,您可以执行以下操作:

<div class='container'>
  <span class="test"></span>
  <span class="test"></span>
  <span class="aaa"></span>
  <span class="test"></span>
</div>

and then the jquery: 然后是jQuery:

$('container span:nth-child(odd)')

and

$('container span:nth-child(even)')

See the jQuery manual for nth-child for more info. 有关更多信息,请参见nth-child的jQuery手册

If this isn't what you're looking for, then I would suggest following @NielsKeurentjes advice and using multiple class names in the relevant elements. 如果这不是您想要的内容,那么我建议遵循@NielsKeurentjes的建议,并在相关元素中使用多个类名。

Hope that helps. 希望能有所帮助。

I would change the classes, because a HTML-Tag can have multiple classes at once: 我将更改类,因为HTML-Tag可以一次包含多个类:

<span class="test"></span>
<span class="aaa"></span>
<span class="test testone"></span>
<span class="test testtwo"></span>
<span class="aaa-one"></span>
<span class="test testtwo"></span>

If you can't change the HTML, you can do it with javascript (jquery): 如果您无法更改HTML,则可以使用javascript(jquery)进行更改:

$('[class]').each(function(){
   var className = $(this).attr("class");
   if("test" == className.substring(0, 4)) //starts with "test"
   {   doSomething();
   }
});

(This code only works, if the tag has not more than one class) (仅当标签的类别不超过一个时,此代码才有效)

But this is dirty code because it scans each dom-element which has a class. 但这是肮脏的代码,因为它会扫描具有类的每个dom元素。 If you only want to apply css-Style, the better solution, if you can't change the HTML, is to add all possible classes to the css-File: 如果只想应用css-Style,则更好的解决方案(如果无法更改HTML)是将所有可能的类添加到css-File中:

.test, .test-one, .test-two{
   ...
}

...or using the css3-selectors as mentioned in the other answers, but older browsers won't support it. ...或使用其他答案中提到的css3-selectors,但较旧的浏览器不支持。

You can use either 您可以使用

$("span[class*='test']"); // element with test anywhere in class attribute

or 要么

$("span[class^='test']"); // element with test at the start in class attribute

note that these will work only if the element has single class. 请注意,这些仅在元素具有单个类时才有效。

however you should better use what @Niels have shown. 但是,您最好使用@Niels显示的内容。

For a good CSS selector reference : Tutorial from net.tutsplus.com 对于一个好的CSS选择器参考: net.tutsplus.com的教程

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

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