Use jquery to remove a div with no children

后端 未结 6 1198
予麋鹿
予麋鹿 2020-12-14 09:23

How can I use jquery to remove a SPECIFIC div that has no children (at least no children that isn\'t whitespace). E.g.

some cont
6条回答
  •  无人及你
    2020-12-14 10:04

    I went with:

    $('#outer > div').filter(function (index) { 
        return $(this).children().length < 1; 
    }).remove();
    

    This says:

    • give me all the div children of #outer
    • use filter to get rid of any which have child nodes
    • remove anything we still have selected

    Sadly, this will remove the div if it contains text, which is probably not what the original poster would have wanted. Plain text doesn't count as a child.

提交回复
热议问题