2013-12-03 2 views
-2

Как удалить th тег с jquery, где класс?удалить th тег с jquery, где класс

мой стол:

<table> 
<thead> 
<th class="NoRemove">1</th> 
<th>2</th> 
<th class="NoRemove">3</th> 
<th class="NoRemove">4</th> 
<th class="NoRemove">5</th> 
<th>6</th> 
<th>7</th> 
</thead> 
</table> 

демо: jsfiddle

Сценарий:

if(th.class!="NoRemove") 
{ 
th.remove(); 
} 

Мне нужно написать код выше для JQuery. Но я понятия не имею, как писать

+0

Используйте '.not()' или ': нет()' методы JQuery для этой задачи. –

ответ

1
//dom ready handler - wait the execution of the passed function till the dom is loaded 
jQuery(function(){ 
    //fetch all the th elements which does not have the class NoRemove and remove it 
    $('th:not(.NoRemove)').remove() 
}) 

См:

Демо: Fiddle

+0

почему $ ('th: not (.NoRemove, .Test)'). Remove() не работает? – user3002842

+0

@ user3002842 работает http://jsfiddle.net/arunpjohny/229FS/2/ –

1

Я хотел бы использовать .filter():

$(function(){ 
    $('table th').filter(':not(.NoRemove)').remove(); 
}); 

DEMO

Однако существуют тысячи различных способов идти об этом! Вот почему jQuery так потрясающе.

Смежные вопросы