如何不填充给定div中的内容

时间:2012-03-16 06:17:10

标签: jquery

<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中的元素说容器2是如何填充的。 在上面的代码中,它填充了div的内部容器2

5 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分: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();
 }
});

答案 2 :(得分:1)

像这样 - &gt;

$(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!'); 
    }
  });
});

答案 3 :(得分:1)

更改您的选择器以排除您不想要的内容:

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

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

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

答案 4 :(得分:1)

$(function(){
  $('body div').each(function() {
    if($(this).attr('id') == 'container2' || $(this).parent().attr('id') == 'container2' ) 
     {  
      //not showing any thing 
     }
     else
     { 
         alert(($(this).html());
     }
  });
});