检查jQuery中DIV的内容是否为空 [英] Check if the content of a DIV is empty in jQuery

查看:418
本文介绍了检查jQuery中DIV的内容是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery检查<div>的内容是否为空?

How do I use jQuery to check to see if the content of a <div> is empty?

我尝试了此操作,但似乎无法打印出正确的值.

I tried this and is doesn't seem to be printing the correct values.

...
var unframed_items = $("#unframed-items");
alert(unframed_items.html().length);
...
<div id="unframed-items"> </div>
...

推荐答案

如果您的意思是真的是空的,请使用 empty-selector [docs] :

If you mean really empty, use the empty-selector[docs] :

alert( !!$("#unframed-items:empty").length )

alert( $("#unframed-items").is(':empty') )

如果您认为仅限空白为空,请使用 jQuery.trim() [docs] 方法:

If you consider whitespace-only to be empty, then use the jQuery.trim()[docs] method:

alert( !$.trim( $("#unframed-items").html() ) );

这篇关于检查jQuery中DIV的内容是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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