2015-09-08 105 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()