使用jquery删除没有孩子的div [英] Use jquery to remove a div with no children

查看:101
本文介绍了使用jquery删除没有孩子的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery删除没有子项(至少没有子项不是空格)的SPECIFIC div。例如

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

<div id="outer">
    some content
    <div id="removeme"> 


    </div>
    some more content
</div>

要完全删除id为removeme的div。

Want to completely remove the div with id="removeme".

推荐答案

要删除 id 等于 removeme

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

要删除 id 等于 removeme 仅当它为空时:

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

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

要删除所有空< div> s:

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

编辑:如果不为空,但有空格:

If it's not empty, but has whitespace:

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

这篇关于使用jquery删除没有孩子的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆