开发者

Return div as a string- but only visible parts

There is a div #allContent that contains a few di开发者_运维百科vs within it. I would like to return the entire #allContent as a string, but remove any divs contained inside that are not visible.

I imagine this can probably be accomplished with some combination of filter(), :visible, and contents(), but I can't figure out exactly how to do it.


var clone = $('#allContent').clone()
                            .appendTo('body')
                            .find(':hidden')
                            .remove()
                            .end()
                            .remove();

var content = clone[0].outerHTML || $('<div>').append(clone).html();

Example: http://jsfiddle.net/ChXPB/3/

EDIT: Changed it to include the outerHTML div.

EDIT: Made it mostly chained, and repaired the Firefox shim.


Seems that we need to temporarily add the clone to the DOM.

This should do it:

$('#allContent').clone().find(':hidden').remove().end().html();


var allContentsHtml = '';
$('#allContent div:visible')
        .each(function() { 
                   allContentsHtml += $(this).html(); 
                   }
              );

$('#output').html(allContentsHtml );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜