2015-10-14 25 views
2

下面刪除特定父的孩子是我的HTML結構從選擇

<div class="parent"> 
//many other elements 
<div class="child"> 
<div class="grandchild"></div> 

</div> 
</div> 

我要選擇除了內部的子元素

我想下面的其中一個沒有工作

裏面父的所有元素
$(".parent *:not(.child *)") 

我該如何選擇?

+0

'$'孩子是這個試過了嗎? – guradio

+0

檢查此解決方案http://stackoverflow.com/questions/1125700/how-do-i-use-jquery-to-select-all-children-except-a-select-element –

回答

2

試試這個:

$('.parent *').filter(function(){ 
     return $(this).parents('.child').length === 0; 
}); 

這將選擇內部.parent所有元素,除了那些誰也爲兒童/的.child

2
$('.parent *:not(.child, .child *)') 

子兒試試這個。

雖然也許你想這樣的:

$(".parent > *:not(.child)") 
1

試試這個: 會選擇所有的父類.parent下與類兒童開始DIV。

$('.parent div[class^="child"]') 
1
$('.parent *').not('.child *') 
1

我要選擇內 子元素除外父裏面所有元素

你可以使用find方法。 它會發現所有的子孫/兒童除外(「母公司*」)的內部類child

$(".parent").find("*").not($(".child").children()).each(function(){ 
alert($(this).prop("class")); 
});