删除div中的所有类

时间:2022-12-04 15:20:49

In outer div I want to remove all the classes testClass but this is not working.

在外部div我想删除所有类testClass但这不起作用。

http://jsfiddle.net/TAcZJ/

http://jsfiddle.net/TAcZJ/

jquery

jQuery的

$('#outer').removeClass('testClass');

html

HTML

 <span class="testClass">d</span>
 <span class="testClass">d</span>
 <span class="testClass">d</span>
 <span class="testClass">d</span>

<div id="outer">
    <span class="testClass">d</span>
    <span class="testClass">d</span>
    <span class="c">d</span>
    <span class="testClass">d</span>
    <span class="testClass">d</span>
    <span class="testClass">d</span>
    <span class="c">d</span>
    <span class="testClass">d</span>
    <span class="testClass">d</span>
    <span class="testClass">d</span>
</div>

css

CSS

.testClass{color:red;}

.c{ color:blue;}

8 个解决方案

#1


6  

$('#outer > .testClass').removeClass('testClass');

You're currently selecting only the wrapper div, not the children spans with the class. I would argue against the other answers, since they are selecting more elements than necessary.

您目前只选择包装器div,而不是孩子跨越该类。我会反对其他答案,因为他们选择的元素多于必要的元素。

#2


6  

Approach 1

$('#outer > .testClass').removeClass('testClass');('testClass');

Approach 2

$('#outer').find('span').removeClass('testClass');

Approach 3

$('#outer').children('span').removeClass

Approach 4

$('#outer').children().removeClass('testClass');

Approach 5

jQuery.grep($('#outer > .testClass'), function (data) {
        $(data).removeClass('testClass');
});

Approach 6

$.each($('#outer > .testClass'), function(index, data){
     $(data).removeClass('testClass');
});

#3


1  

You can do this :

你可以这样做 :

$('#outer').children().removeClass('testClass');

#4


1  

$('#outer span').removeAttr('class');

#5


0  

try this

尝试这个

$('#outer > span').removeClass('testClass'); 

or

要么

  $('#outer  span').removeClass('testClass');

LINK

链接

#6


0  

document.getElementById("outer").innerHTML = document.getElementById("outer").innerHTML.replace(/testClass/g, '');

Just use some VanilaJS ;-) should be enough.

只需使用一些VanilaJS ;-)应该就够了。

#7


0  

$('#outer span').removeClass('testClass');

or

要么

$('#outer .testClass').removeClass('testClass');

or

要么

$('#outer').find('.testClass').removeClass('testClass');

#8


-1  

Try :

试试:

$('#outer  span').removeClass('testClass');

#1


6  

$('#outer > .testClass').removeClass('testClass');

You're currently selecting only the wrapper div, not the children spans with the class. I would argue against the other answers, since they are selecting more elements than necessary.

您目前只选择包装器div,而不是孩子跨越该类。我会反对其他答案,因为他们选择的元素多于必要的元素。

#2


6  

Approach 1

$('#outer > .testClass').removeClass('testClass');('testClass');

Approach 2

$('#outer').find('span').removeClass('testClass');

Approach 3

$('#outer').children('span').removeClass

Approach 4

$('#outer').children().removeClass('testClass');

Approach 5

jQuery.grep($('#outer > .testClass'), function (data) {
        $(data).removeClass('testClass');
});

Approach 6

$.each($('#outer > .testClass'), function(index, data){
     $(data).removeClass('testClass');
});

#3


1  

You can do this :

你可以这样做 :

$('#outer').children().removeClass('testClass');

#4


1  

$('#outer span').removeAttr('class');

#5


0  

try this

尝试这个

$('#outer > span').removeClass('testClass'); 

or

要么

  $('#outer  span').removeClass('testClass');

LINK

链接

#6


0  

document.getElementById("outer").innerHTML = document.getElementById("outer").innerHTML.replace(/testClass/g, '');

Just use some VanilaJS ;-) should be enough.

只需使用一些VanilaJS ;-)应该就够了。

#7


0  

$('#outer span').removeClass('testClass');

or

要么

$('#outer .testClass').removeClass('testClass');

or

要么

$('#outer').find('.testClass').removeClass('testClass');

#8


-1  

Try :

试试:

$('#outer  span').removeClass('testClass');