【问题标题】:How to recursively remove CSS classes如何递归删除 CSS 类
【发布时间】:2014-12-10 08:23:58
【问题描述】:

我正在尝试使用 jQuery 从 DIV 中递归删除所有 CSS 类。

这是我到目前为止所得到的,但它根本不起作用

http://jsfiddle.net/qxy7yotj/4/已更新

//更新:另一个问题是我将 HTML 作为 JAvascript 字符串,我必须以这种方式操作它

HTML:

<div class="c1">
  <div class="c1">
    <div class="c3">some text
      <div>blah blah</div>
    </div>
  </div>
</div>

JQuery:

$('div').removeClass();

$('div').each(function( index ) {
    $(this).removeClass();
});

【问题讨论】:

  • 您只需将 jQuery 添加到您的小提琴中,语法 $('div').removeClass(); 就可以了...

标签: jquery css


【解决方案1】:

您需要添加jQuery to your fiddle,因为这是您编写 Javascript 的语法。

那么你只需要使用:

$('div').removeAttr('class');

【讨论】:

  • 感谢回复,请看更新问题
  • @Igor...您的更新类型改变了问题的性质!
【解决方案2】:

首先,动态创建一个元素,然后在其中搜索:

var s = '<div class="c1"> <div class="c1"><div class="c3">some text<div>blah blah</div></div></div></div>'

var $elem =
    // create on the fly element
    $('<div></div>')

        // put your string to its content
        .html(s)

        // find `div` elements
        .find('div')

            // remove `class` attribute
            .removeAttr('class');

$('#id').html($elem.html());

JSFiddle Demo.

【讨论】:

    【解决方案3】:

    使用removeAttr('class') 并在您的 Fiddle 中加载 jQuery 检查DEMO

    $('div').each(function() {
       $(this).removeAttr('class');
    });
    

    【讨论】:

    • 感谢回复,请看更新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2023-01-08
    • 2016-05-30
    • 2011-02-08
    • 2017-05-12
    • 2014-11-10
    相关资源
    最近更新 更多