当类名以某个单词开头时,选择一个元素 [英] Select an element when the class name starts with a certain word
本文介绍了当类名以某个单词开头时,选择一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我具有以下元素:
<div class="home">
<div class="tab231891230"></div>
<div class="tab121232441"></div>
<div class="tab123134545"></div>
</div>
如何使用jQuery选择类以"tab"
开头的div元素?
How can I use jQuery to select the div element that the class starts with "tab"
?
推荐答案
它称为属性以选择器开头.我的示例在元素上设置了红色文本颜色:
It is called the Attribute Starts With Selector. My example sets a red text color on the elements:
$('[class^="tab"]').css('color', 'red');
请注意,如果元素具有一个以上的类,而另一个元素在其中一个带有tab
的类之前(class="nyedva tab231891230"
),则该选择器将不会选择该元素.
Please note that if the elements have more than one class and the other precedes the one with tab
inside (class="nyedva tab231891230"
) the element won't be selected by this selector.
如果甚至要选择这些,您可以使用以下示例:
If you want to select even these, you can use this example:
$('.home div').filter(function () {
return this.className.match(/\btab/);
}).css('color', 'red');
这篇关于当类名以某个单词开头时,选择一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文