【问题标题】:Remove child class from active div [duplicate]从活动 div 中删除子类 [重复]
【发布时间】:2015-05-03 18:16:27
【问题描述】:

我无法从活动 div 中仅删除 img 类:

<div class="thumb thumb-selected">

<img class="img-thumb BWFilter BWfade" src="100x100.jpg" alt=""><img crossorigin="anonymous" style="height: 100px; width: 100px; margin-top: 0px; margin-left: 0px;" class="img-thumb" src="3-100x100.jpg" alt=""></div>

我想删除 (BWFilter BWfade) 类,只针对这个 div (.thumb .thumb-selected),我正在尝试使用这个 jquery 代码:

$('.thumb .thumb-selected').find('.img-thumb img').removeClass("BWFilter BWfade");

【问题讨论】:

  • 试试这个 $('div.thumb').find('img').removeClass("BWFilter BWfade");
  • 请使用下面给出的 JS 代码,希望它能解决您的问题
  • 使用'.thumb.thumb-selected' 作为选择器(即删除空格)来查找具有两个类的元素。
  • 感谢您的请求,但我只需要找到选定的 div 并仅删除选定 div 的类,在这种情况下,代码从所有拇指 div 中删除 BWFilter BWfade

标签: jquery html


【解决方案1】:

removeClass 接受多个要删除的类:

 $('.thumb.thumb-selected > img.img-thumb').removeClass('BWFilter BWfade');

&gt; - 选择器仅在 img 是您的 div 的直接子级时匹配。

参考

.removeClass()

css - child-selector

【讨论】:

  • 像魅力一样工作!谢谢!
【解决方案2】:

考虑这个 jquery 行:

$('.thumb.thumb-selected').find('img.img-thumb').removeClass("BWFilter BWfade");

DEMO

$('.thumb .thumb-selected') 中删除空间。

参考:How can I select an element with multiple classes?

【讨论】:

    【解决方案3】:

    使用这个。更少的代码是肯定的。第一个选择器将只选择具有.thumb AND .thumb-selected 类名的div 元素。那么,.find()函数就是获取当前匹配元素集合中每个元素的后代。对于这种情况,会找出img tag,并去掉附在上面的.BMFilter and .BWfade类名。

    $('div.thumb.thumb-selected').find('img').removeClass("BWFilter BWfade");
    

    【讨论】:

    • 谢谢,但是当 div.thumb 被选中并选择了类 thumb-selected 时,我只想删除 BWFilter 和 BWfade 类
    • 是这样的吗? $('div.thumb.thumb-selected').find('img').removeClass("BWFilter BWfade");请参阅已编辑的帖子。
    • 请为您的答案添加一些解释。
    • 完成。答案已编辑。
    【解决方案4】:

    试试这个

    $('.thumb-selected').find('.BWFilter').removeClass("BWFilter").removeClass("BWfade");
    

    【讨论】:

    • 谢谢,我只是尝试这个解决方案,但什么也没有:(如果 find() 到达 .thumb-selected,我该如何测试?谢谢!
    • 你可以使用console.log($('.thumb-selected'));
    【解决方案5】:

    试试这个:-

    $('div.thumb.thumb-selected').find('img.img-thumb').removeClass("BWFilter BWfade");
    

    【讨论】:

      【解决方案6】:

      使用这个

      $('.thumb.thumb-selected').find('img.img-thumb').removeClass("BWFilter BWfade");
      

      【讨论】:

        【解决方案7】:

        请使用下面给出的JS代码

        $('.thumb-selected').find('.img-thumb').removeClass("BWFilter").removeClass(' BWfade');
        

        【讨论】:

        • 谢谢,我试过这个解决方案,但没有:(
        猜你喜欢
        • 2021-03-22
        • 1970-01-01
        • 2013-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-13
        • 2011-10-30
        • 1970-01-01
        相关资源
        最近更新 更多