开发者

How can I iteratively remove each leaf DOM element in a page using jQuery?

How can i in jquery itirate the whole dom, delete element that have no开发者_开发知识库t children and so on and son on until the page is empty ! wouaaah


You can try something like this

$('*').each(function({
    $(this).remove();
});


try this, but it is not iterate:

var all = $('*');
while (all.length) {
    $(':empty').remove();
    all = $('*');
}


Haven't tested it... but I'm thinking something like this

$('body').children().each(function(){
    recursiveDestroyer(this);
});



function recursiveDestroyer(element){
    $(element).each(function(){
        if ($(element).children().size() == 0){
             $(element).hide();
        } else {
           return recursiveDestroyer($(element).children());  
        }
    }
}

EDIT: Ah -- I'm too tired to fix it going to bed now, but i think this needs to be added and have the current loop placed within the if statement below

var hiddenCount = 0;
$(element).each(function(){
   if ($(this).is(":hidden")) { hiddenCount++; }
});

if (hiddenCount != $(element).size()){
  // insert the original $(element).each(.. function here
} else {
  return true;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜