【发布时间】:2017-05-23 10:58:02
【问题描述】:
我想突出显示任何具有以a_slot_ 开头的类但在任何类名中都不以pink 或green 结尾的元素。在这个例子中,我觉得应该突出显示第三个元素,因为它有类a_slot_orange,而另一个类不包含pink 或green。
为什么第三个元素没有突出显示(但最后一个元素是)?如何与其他人一起突出显示第三个?
$(document).ready(function() {
$("[class^='a_slot_']").each(function(i,v){
if(!$(this).attr('class').includes('green') && !$(this).attr('class').includes('pink')){
$(this).css('background-color', 'red');
}
});
// also don't work....have the same result
//$("[class^='a_slot_']:not([class$='green'],[class$='pink'])").css('background-color', 'red');
//$("[class^='a_slot_']").not("[class$='green'],[class$='pink']").css('background-color', 'red');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class='a_slot a_slot_green'>Green</p>
<p class='a_slot a_slot_pink'>Pink</p>
<p class='a_slot a_slot_orange'>Orange (shouldn't I be highlighed too?)</p>
<p class='a_slot_green'>Green</p>
<p class='a_slot_pink'>Pink</p>
<p class='a_slot_orange'>Orange</p>
<p class='a_slot_4'>4</p>
<p class='a_slot_16'>16</p>
<p class='a_slot_16 other_class'>16</p>
<p class='a_slot_orange other_class'>Orange</p>
【问题讨论】:
-
前3段不以a_slot_开头
-
你可能想使用
$("[class*='a_slot_']")。 -
@Gerard 我知道,但是第二课可以
-
@depperm 但您使用的是以选择器开头的属性
-
@billyonecan 我认为课程的顺序无关紧要
标签: javascript jquery html jquery-selectors