使用jquery,如果内容为空,如何删除DIV ?

时间:2021-12-10 08:50:33

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. For display reasons I need to remove it when it's empty.

下面的div有时候会输出没有内容的内容。出于显示原因,我需要在它为空时删除它。

<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.

我一直在尝试下面的和它的变化,但是没有任何运气。

2 个解决方案

#1


7  

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. but you won't see this when you do it because the div is empty. ;-)

注意:我已经设计了div,所以在js生效之前,您可以看到它的一个简短的flash。但是当你这么做的时候你不会看到这个,因为这个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();

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

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

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

#2


0  

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:

删除所有空

s:

$("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。

#1


7  

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. but you won't see this when you do it because the div is empty. ;-)

注意:我已经设计了div,所以在js生效之前,您可以看到它的一个简短的flash。但是当你这么做的时候你不会看到这个,因为这个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();

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

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

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

#2


0  

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:

删除所有空

s:

$("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。