Use jquery to remove a div with no children

后端 未结 6 1161
予麋鹿
予麋鹿 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:01

    To remove the element with id equal to removeme:

    $("#removeme").remove();
    

    To remove the element with id equal to removeme only if it is empty:

    $("#removeme:empty").remove();
    

    To remove all empty

    s:

    $("div:empty").remove();
    

    EDIT: If it's not empty, but has whitespace:

    if($.trim($("#removeme").text()) == "") {
      $("#removeme").remove();
    }
    

提交回复
热议问题