2015-09-08 104 views

回答

3

使用.filter().remove()功能,像这样:

$('p').filter(function(){ 
    return ($(this).text() == "") 
}).remove(); 

或者使用.each()

$('p').each(function (i, e) { 
if ($(e).text() === "") $(e).remove(); 
}); 

更多更好地利用:empty选择:

$('p:empty').remove(); 

DEMO - 检查元件(铬),以查看最终输出

1

试着做这样的:

$('p') 
    .filter(function() { 
     return $.trim($(this).text()) === '' && $(this).children().length == 0 
    }) 
    .remove()