开发者

Determining if a div contains a table element in JQuery

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜