2015-06-25 59 views
1

我有動態UL李節點,使樹,我想刪除它有刪除所有子節點與specfic類

class="highlight" or 
class="add_action" or 
class="delete_action" or 
class="edit_action" 

下與specfic ID specfic裏所有子跨度節點 - (在本例中20) - 我想這代碼與jQuery找到與論文類的所有跨刪除它,但它並沒有工作

$('li#20').find('span.add_action').each(function(){ 
    $(this).remove(); 
}); 

還累

$('li#20').eq(0).find('.add_action').remove(); 

    $('li#20').children('.add_action').remove(); 

此完整示例

https://jsfiddle.net/kqagjtmr/

+1

您尚未在該小提琴中加載jQuery。 – undefined

回答

2

你在你的代碼的重複id屬性,例如:

<li class="thide" id="20"><span class="vertical"></span> 

<span id="20" class="first_name" title="">الجد سعد</span> 

這就是爲什麼$('li#20')不能正常工作。 id屬性必須是唯一的,並且不應以數字開頭。改用類。

remove元素,只需使用:

$('someSelector').remove(); 

此外,你應該包括jQuery的在你的小提琴,你可以在這裏找到的選項:

enter image description here

+0

你真棒+1爲你的男人:)。我知道我不是OP,但我喜歡你的答案:P – squiroid

+1

@squiroid:謝謝,非常感謝。 – Cerbrus

2

獲取具有指定類的所有span元件和刪除所有這些的。不需要使用each來循環。使用逗號分隔的span元素選擇所有匹配的元素。

$('li#20') 
    .find('span.highlight, span.add_action, span.delete_action, span.edit_action') 
    .remove(); 

DEMO

1

請參閱更新的小提琴「 https://jsfiddle.net/kqagjtmr/1/

使用$('li#20').find("highlight add_action delete_action edit_action").remove();

一次查找多個元素並將其刪除。

+0

爲什麼upvote,當這不起作用 – Tushar