0
是否有可能選擇與this
上下文使用多個元素,如選擇具有此背景下多類
$(this,this.parent,'.other').removeClass('test').addClass('test2');
我想選擇this
元素+ this
父元素+其他一些類的所有togather
是否有可能選擇與this
上下文使用多個元素,如選擇具有此背景下多類
$(this,this.parent,'.other').removeClass('test').addClass('test2');
我想選擇this
元素+ this
父元素+其他一些類的所有togather
你不能調用.parent()選擇器內,使用:
$(this).parent().add(this).add('.other').removeClass('test').addClass('test2');
示例:
$("b").each(function() {
$(this).parent().add(this).add('.other').removeClass('test').addClass('test2');
});
.test {
color: red;
}
.test2 {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="test">Parent <b class="test">this</b></p><i class="other test">Other</i>
是的,工作。至少這比寫多行更好 –