开发者

Determining if a div contains a table element in JQuery

开发者 https://www.devze.com 2022-12-28 05:36 出处:网络
For my accordion, each time the user clicks on the header, i want to see if accordion-content co开发者_运维百科ntains a table, if not, generate one and add it to accordion-content. But how do you chec

For my accordion, each time the user clicks on the header, i want to see if accordion-content co开发者_运维百科ntains a table, if not, generate one and add it to accordion-content. But how do you check if the current element contains a certain type of element?

   <div class="a-header"> 
      <ul><li></li></ul>
    </div>
    <div class="a-content">
    </div>

    $('.a-header').click(function(){
       var currentHeader = $(this).next();
       //if currentHeader contains 'table'
});

Thanks.


You're almost there:

 $('.a-header').click(function(){
   var currentHeader = $(this).next();
   if (currentHeader.find('table').length > 0) {
       //do stuff
   }
});


both these are good, but a faster selector is

$(this).next(':not(:has(table))');


$('.a-header').click(function() {
  if (!$(this).next().has('table').length) {
    // CREATE THE TABLE
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消