[英]How do I change one element change without affecting other elements with the same class?
[英]How to remove a class from one set of siblings without affecting other elements with same class
我有以下几点:
<div class="attributes_set_1>
<label class="attributes"><input type="radio" value="1" /></label>
<label class="attributes"><input type="radio" value="2" /></label>
<label class="attributes"><input type="radio" value="3" /></label>
</div>
<div class="attributes_set_2>
<label class="attributes"><input type="radio" value="1" /></label>
<label class="attributes"><input type="radio" value="2" /></label>
<label class="attributes"><input type="radio" value="3" /></label>
</div>
我试图在单击时将活动类添加到第一组单选按钮中的任何一个,但是如果从同一组中选择一个不同的类,则也要删除该类。 问题是我需要能够从下一组单选按钮中进行选择,而不必从其他任何一组中删除活动的类。
我尝试了几种选择器,但我认为使其工作的方法是选择该特定父类的孩子。
我如何仅选择特定父类的兄弟姐妹以删除活动类?
$('.attributes').children().removeClass('active');
一旦使removeClass()工作,如何将活动类添加到我单击的任何其他无线电中,而不管它是否属于同一个兄弟姐妹?
$(this).addClass('active');
感谢任何人的帮助。
假设this
是您添加了active
类的单选按钮,则可以找到父label
元素的同级,然后从其输入元素中删除该类
$(this).parent().siblings().find('input').removeClass('active');
尝试这个:
$('.attributes input').click(function() {
$(this).addClass('active').parent().siblings('.attributes').find('input').removeClass('active');
})
尝试这个:
$(this).parent().siblings().find('.active').removeClass("active");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.