2012-03-16 60 views
0
<body> 
<div id = "container1" > 
<div id = "x1" /> 
<div id = "x2" /> 
<div id = "x3" /> 
</div> 

<div id = "container2" > 
<div id = "Y1" /> 
<div id = "Y2" /> 
<div id = "Y3" /> 
</div> 

<div id = "container3" > 
<div id = "Z1" /> 
<div id = "Z2" /> 
</div> 
</body> 

$('body div').each(function(i) { 
if($(this).attr('id')!='container2') { //This is only for div container2 & not for elements inside it 
    console.log(i); 
} 
}); 

我不希望特定的div內的元素說container2的要填充如何才能做到這一點.. 在上面的代碼裏面填充container2的如何不填充給定的div裏面的內容

div的
+2

填充數據意味着什麼?你想隱藏特定div的內容嗎?或者清空那個div的數據? – San 2012-03-16 06:24:32

+1

可以有多種方式,在您的_if_條件中添加另一個檢查,以檢查內部div的父級是否不是container2。這不是測試:!'$(本).parent()ATTR( '身份證')='container2'' – Coder 2012-03-16 06:31:09

+0

@聖:我其實排空DIV – user1184100 2012-03-16 06:48:13

回答

1

改變你的選擇,以排除不想要的東西:

排除 'container2的':$('body div[id!="container2"]')

排除所有的div用容器中的:$('body div').not('[id*="container"]')

http://api.jquery.com/category/selectors/

1

試試這個:

$('body div').each(function(i) { 
    if($(this).attr('id')!='container2') { 
     console.log($(this).clone().empty()); 
    } 
    }); 
1

如果我正確理解你的問題,這應該制定出適合你:

$('body div').each(function(i) { 
if($(this).attr('id').indexOf("container") !== -1) { 
    //this is a container element. It could be ANY container. 

    //you could check for which container like   
    $(this).attr('id')//returns the container id 

    //if you want its children 
    $(this).children(); 
} 
}); 
+0

感謝gideon .. – user1184100 2012-03-16 07:08:57

1

喜歡這樣 - >

$(function(){ 
    $('body div').each(function(i,value) { 
    if($(value).attr('id') == 'container2') { //This is only for div container2 & not for elements inside it 
     console.log(i); 
    }else{ 
    console.log('these are not the container!'); 
    } 
    }); 
}); 
1
$(function(){ 
    $('body div').each(function() { 
    if($(this).attr('id') == 'container2' || $(this).parent().attr('id') == 'container2') 
    { 
     //not showing any thing 
    } 
    else 
    { 
     alert(($(this).html()); 
    } 
    }); 
}); 
+0

謝謝kanishka – user1184100 2012-03-16 07:08:05

+0

@ user1184100,歡迎您:D:D – 2012-03-16 07:08:54