【问题标题】:Remove CSs after click in jQuery [duplicate]在jQuery中单击后删除CS [重复]
【发布时间】:2013-02-28 03:15:18
【问题描述】:

我有这个结构:

$('.user_adr_edit div a').click(function(){
$(this).css('width', '210px');
});

它正在工作。但我想,当再次点击时,删除width:210px

必须使用unbindremove或其他解决方案?

【问题讨论】:

  • 是否需要切换宽度..?
  • 您要切换功能吗?那么1.点击:添加宽度,2.点击:删除宽度,3.点击:添加宽度等等?

标签: function jquery


【解决方案1】:

你应该使用 css 规则来改变 css 属性

CSS

a.wide{
   width:210px;
}

只需添加/删除类

$('.user_adr_edit div a').click(function(){
    $(this).toggleClass('wide');
});

【讨论】:

    【解决方案2】:
    $('.user_adr_edit div a').click(function(){
       if($(this).css('width') != '210px'){
          $(this).css({'width':'210px'});
       }
       else{
          $(this).css({'width':'0px'});
       }
    });
    

    【讨论】:

    • @zenith 老实说我从未使用过切换。
    • 但是是的,就像@GabyakaG.Pertioli 答案中的切换看起来是一种更好的方法
    • 我不会:toggle 自 1.8 以来已被弃用,并在 1.9 中被删除:api.jquery.com/toggle-event
    • @cdmckay 感谢您提供的信息!现在我很高兴我从来没有开始使用它。
    • 另外最好使用.css('width','') 恢复到之前的状态,而不是设置为0
    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多