使用if语句检查div是否为空 [英] Using an if statement to check if a div is empty
本文介绍了使用if语句检查div是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果单独的div为空,我正在尝试删除特定的div.这是我正在使用的:
I'm trying to remove a specific div if a separate div is empty. Here's what I'm using:
$(document).ready(function () {
if ('#leftmenu:empty') {
$('#menuTitleWrapper').remove();
$('#middlemenu').css({ 'right': '0', 'position': 'absolute' });
$('#PageContent').css({ 'top': '30px', 'position': 'relative' });
}
});
我认为这已经很接近了,但是我不知道如何编写代码来测试#leftmenu是否为空.感谢您的帮助!
I think this is close but I can't figure out how to write the code to test of #leftmenu is empty. Any help is appreciated!
推荐答案
您可以使用 .is()
.
You can use .is()
.
if( $('#leftmenu').is(':empty') ) {
// ...
或者您可以仅测试 length
属性以查看是否找到了该属性.
Or you could just test the length
property to see if one was found.
if( $('#leftmenu:empty').length ) {
// ...
请记住,空也不表示空格.如果可能会有空白,则可以使用 $.trim()
并检查内容.
Keep in mind that empty means no white space either. If there's a chance that there will be white space, then you can use $.trim()
and check for the length of the content.
if( !$.trim( $('#leftmenu').html() ).length ) {
// ...
这篇关于使用if语句检查div是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文