简体   繁体   English

使用jquery如果DIV的内容为空,我怎么能删除它?

[英]Using jquery how can I remove a DIV if it's contents are empty?

I'm looking for a way to remove the following empty div using jquery. 我正在寻找一种方法来使用jquery删除以下空div。

The following div is output sometimes without content inside it. 有时输出以下div而不包含内容。 For display reasons I need to remove it when it's empty. 出于显示原因,我需要在空的时候将其删除。

<div class="field-group-format group_call_to_action_aside div group-call-to-action-aside box yellow speed-fast effect-none"></div>

Thoughts? 思考?

I've been trying the following and variations of it, but have't had any luck. 我一直在尝试以下和它的变化,但没有运气。

This works, assuming you don't have other div.group_call_to_action_aside that you want to keep: 这是有效的, 假设您没有要保留的其他div.group_call_to_action_aside

if ($('.group_call_to_action_aside').is(':empty')) { 
    $('.group_call_to_action_aside').remove();
} 

Note: I've styled the div , so you can see a brief flash of it before the js takes effect. 注意:我已经设计了div ,所以在js生效之前你可以看到它的短暂闪现。 but you won't see this when you do it because the div is empty. 但是当你这样做时你不会看到这个,因为div是空的。 ;-) ;-)

http://jsfiddle.net/jasongennaro/L8dwn/ http://jsfiddle.net/jasongennaro/L8dwn/

EDIT 编辑

Yes, as per your commment, you can also do this 是的,根据你的意见,你也可以这样做

$('.group_call_to_action_aside:empty').remove();

http://jsfiddle.net/jasongennaro/L8dwn/1/ http://jsfiddle.net/jasongennaro/L8dwn/1/

To remove the element with id equal to removeme : 要删除id等于removeme的元素:

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

To remove the element with id equal to removeme only if it is empty: 要删除id等于removeme的元素,只有它是空的:

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

To remove all empty <div> s: 要删除所有空<div>

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

EDIT: If it's not empty, but has whitespace: 编辑:如果它不是空的,但有空格:

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

Reference: Use jquery to remove a div with no children 参考: 使用jquery删除没有子节点的div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM