1

スクリプトに少し問題があります。スパンを確認する必要があります。途中にコンテンツがある場合は何もしません...しかし、空の場合は、このスパンを削除するか、たとえばスタイルを変更します。スパンが空であることを指定すると、スクリプトはすべてのスパンを消去します。手伝って頂けますか?

html

<span class="insert_list_item"></span>
<span class="insert_list_item">data</span>

脚本

if ($('.insert_list_item').length > 0){
    $('.insert_list_item').each().remove();
} 
4

5 に答える 5

4

使用する:empty

$('.insert_list_item:empty').remove()

すべての空の要素を削除します。または

 $('.insert_list_item:not(:empty)').remove()

空でない要素をすべて削除するには

http://api.jquery.com/category/selectors/で、利用可能なすべてのセレクターを確認できます。

于 2012-08-08T14:28:31.463 に答える
2

各チェック長:

$('.insert_list_item').each( function(){
     var span = $(this);
     if (span.html().length === 0) {
         span.remove();
     }
});

また

:empty でフィルタ:

$('.insert_list_item').filter(":empty").remove();
于 2012-08-08T14:30:10.387 に答える
2

どうですか:

$( '.insert_list_item' ).filter(function () { 
    return $.trim( $( this ).text() ) === '';
}).remove();

ライブデモ: http://jsfiddle.net/RMZLm/

このソリューションは、空白を含む SPAN 要素 (例: <span class="insert_list_item"> </span>) でも機能することに注意してください。

于 2012-08-08T14:30:47.293 に答える
1

使用状態 内部each。それぞれがセレクターによって返されたすべての要素を反復します。

$('.insert_list_item').each(function(){

  if($(this).html().length === 0)
       $(this).remove();
});
于 2012-08-08T14:28:53.223 に答える
0

これを試して:

if ($('.insert_list_item').text().length > 0){
    $('.insert_list_item').remove();
} 
于 2012-08-08T14:29:28.637 に答える