删除最近的标签样式

时间:2017-02-24 13:31:50

标签: jquery

当我点击标签时我有3个标签需要添加bg颜色。其他标签的颜色已删除。如何做到?



with open("file.txt") as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
text = [x.strip() for x in content] 

x = [i.split(";") for i in text]
x.sort(key=lambda x: x[2])
from itertools import groupby
from operator get itemgetter
y = groupby(x, itemgetter(2))
res = [(i[0],[j for j in i[1]]) for i in y]
for country in res:
     with open(country[0]+".txt","w") as writeFile:
             writeFile.writelines("%s\n" % ';'.join(l) for l in country[1])

$(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');
  });
});




3 个答案:

答案 0 :(得分:3)

由于您的其他标签是兄弟姐妹,因此您可以使用jQuery中的siblings方法。



$(document).ready(function(){
    $('.btn').click(function(){
        var $this = $(this);
        $this.css({'background':'#2196F3', 'color':'#fff'});
        $this.siblings("label").removeAttr('style');
    });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="btn">
    <input type="radio" id="agent" name="advanced-search-beds" checked>Agent
</label>
<label class="btn">
    <input type="radio" id="owner" name="advanced-search-beds">Owner
</label>
<label class="btn">
    <input type="radio" id="other" name="advanced-search-beds">Other
</label>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你不应该使用$(this).closest('label').removeAttr('style');,因为应用于&#34;点击了&#34;一。你需要把它删除到所有标签,不包括点击的标签。例如:

$(this).css({'background':'#2196F3', 'color':'#fff'});
$('label').not(this).removeAttr('style');

看看我的例子:https://jsfiddle.net/zLsnatck/

答案 2 :(得分:1)

这样的东西?

$(document).ready(function(){
                $('.agentBtn').click(function(){
                    $(this).closest('div').find('label').removeAttr('style');
                    $(this).css({'background':'#2196F3', 'color':'#fff'});
                    
                });
                $('.ownerBtn').click(function(){
                    $(this).closest('div').find('label').removeAttr('style');
                    $(this).css({'background':'#2196F3', 'color':'#fff'});
                });
                $('.otherBtn').click(function(){ 
                    $(this).closest('div').find('label').removeAttr('style');                                                                  
                    $(this).css({'background':'#2196F3', 'color':'#fff'});
                });
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <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>
</div>