Get all elements without child node in jQuery

前端 未结 4 606
忘掉有多难
忘掉有多难 2020-11-27 19:33

I need to select elements without child node (including text since in

text is a child node).

I used empty, but it also consider

4条回答
  •  醉话见心
    2020-11-27 20:00

    Try this

    $("span").each(function(){
    if($(this).text().trim()=='')
    $(this).text('Empty');
    });
    

    If you don't like each function then

    $("span").html(function(){
    if($(this).text().trim()=='')
    return 'Empty';
    });
    

提交回复
热议问题