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>
是的,工作。至少这比写多行更好 –