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
}
});
精彩评论