我有3个标签,当我点击标签bg颜色需要添加和颜色删除其他labels.how要做到这一点?删除最接近的标签样式
$(document).ready(function() {
$('.agentBtn').click(function() {
$(this).css({
'background': '#2196F3',
'color': '#fff'
});
$(this).closest().find('label').removeAttr('style');
});
$('.ownerBtn').click(function() {
$(this).css({
'background': '#2196F3',
'color': '#fff'
});
$(this).closest('label').removeAttr('style');
});
$('.otherBtn').click(function() {
$(this).css({
'background': '#2196F3',
'color': '#fff'
});
$(this).closest().find('label').removeAttr('style');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="btn agentBtn">
<input type="radio" id="agent" name="advanced-search-beds" checked >Agent
</label>
<label class="btn ownerBtn">
<input type="radio" id="owner" name="advanced-search-beds">Owner
</label>
<label class="btn otherBtn">
<input type="radio" id="other" name="advanced-search-beds">Other
</label>
我有同样的想法,但我认为使用'closest'或'siblings'比较好,因为很可能会在可能发生碰撞的形式,不同的标签。 – sniels
是的,兄弟姐妹也可以选择使用,但他的代码非常基本,他希望删除所有其他标签css,不仅是最接近的,反正在另一个标签不存在问题,他应该只添加父类目标 – TriForce