2012-03-16 33 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