本文摘自PHP中文网,作者coldplay.xixi,侵删。
jquery判断元素内容是否为空的方法:1、使用【if(value.length == 0){}】方法,如果value为空执行的操作;2、使用【if(value!=''){}】方法,如果value不为空执行的操作。

本教程操作环境:windows7系统、jquery3.2.1版本,该方法适用于所有品牌电脑。
jquery判断元素内容是否为空的方法:
input 用val();
var value = $('#test').val();
是否为空的判断方法:
jQuery验证文本框内容不为空
通过$.fn
扩展jQuery方法
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$.fn.validate = function (tips){
if ($(this).val() == "" || $.trim($(this).val()).length == 0){
alert(tips + "不能为空!" );
throw SyntaxError();
}
}
|
html元素用html();
var value = $('#test').html();
是否为空的判断方法:
if(value.length == 0){
也可以通过判断是说有没有子节点?对于html获取的方法可用
$('#list').children().length === 0
方法一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <script type= "text/javascript" src= "http://m.jb51.net/skin/mobile/js/jquery.min.js" ></script>
<div><ul id= "thelist2" >
<li><a href= "https://m.jb51.net/game/140209.html" ><img src= "//img.jbzj.com/do/uploads/litimg/140228/100331632c.jpg" >天天飞车航哥破解版</a><em class = "xj star5" ></em></li>
<li><a href= "https://m.jb51.net/game/143515.html" ><img src= "//img.jbzj.com/do/uploads/litimg/140314/0944332514F.jpg" > 节奏大师全P破解版</a><em class = "xj star6" ></em></li>
<li><a href= "https://m.jb51.net/game/207971.html" ><img src= "//img.jbzj.com/do/uploads/litimg/140821/11594R51423.gif" >海岛奇兵国服内购破解版</a><em class = "xj star5" ></em></li>
<li><a href= "https://m.jb51.net/game/144709.html" ><img src= "//img.jbzj.com/do/uploads/litimg/140318/161504236013.gif" >天天炫斗破解版</a><em class = "xj star5" ></em></li>
<li><a href= "https://m.jb51.net/game/80896.html" ><img src= "//img.jbzj.com/do/uploads/litimg/130503/1J21Va46.jpg" >完美女友完整版</a><em class = "xj star5" ></em></li>
</ul>
<div><ul id= "thelist3" ></ul>
<script>
alert($( '#thelist2' ).children().length)
alert($( '#thelist3' ).children().length)
$thelist3 = $( '#thelist3' );
if ( $thelist3 .children().length==0){
}
</script>
|
方法二、
1 2 3 4 5 6 7 | String.prototype.isEmpty = function () {
var s1 = this.replace(/[\r\n]/g, '' ).replace(/[ ]/g, '' ),
s2 = (s1 == '' ) ? true : false;
return s2;
};
$list .html().isEmpty();
if ( $( "#list" ).html() === "" ){} 用三个等号更符合
|
相关免费学习推荐:JavaScript(视频)
以上就是jquery如何判断元素内容是否为空的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
jQuery的easyui和layui区别是什么
jQuery如何判断是否存在class
如何使用jQuery对属性进行获取、设置和删除
如何把jQuery的值放入img标签的src里面
jQuery可见性过滤选择器有哪些
jQuery的$().each和$.each的区别是什么?
jQuery如何判断input是否被禁用
jQuery怎么让select不可选
jQuery怎么实现左右滑动的toggle
jQuery判断函数是否存在的方法
更多相关阅读请进入《jQuery》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » jquery如何判断元素内容是否为空