<分区>
我想突出显示具有以 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>