jquery怎么验证元素是否为空

小编给大家分享一下jquery怎么验证元素是否为空,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了漯河免费建站欢迎大家使用!

jquery验证元素是否为空的方法:1、input用【val()】判断,代码为【var value = $('#test').val()】;2、用【html()】判断,代码为【var value = $('#test').html()】。

jquery验证元素是否为空的方法:

input 用val();

var value = $('#test').val();

是否为空的判断方法:

  • if(value.length == 0){} 如果value为空执行的操作

  • if(value!=''){} 如果value不为空执行的操作

html元素用html();

var value = $('#test').html();

是否为空的判断方法:

if(value.length == 0){

也可以通过判断是说有没有子节点?对于html获取的方法可用

$('#list').children().length === 0

方法一

<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="/upload/otherpic58/100331632c.jpg">天天飞车航哥破解版</a><em class="xj star5"></em></li>
<li><a href="https://m.jb51.net/game/143515.html"><img src="/upload/otherpic58/0944332514F.jpg"> 节奏大师全P破解版</a><em class="xj star6"></em></li>
<li><a href="https://m.jb51.net/game/207971.html"><img src="/upload/otherpic58/11594R51423.gif">海岛奇兵国服内购破解版</a><em class="xj star5"></em></li>
<li><a href="https://m.jb51.net/game/144709.html"><img src="/upload/otherpic58/161504236013.gif">天天炫斗破解版</a><em class="xj star5"></em></li>
<li><a href="https://m.jb51.net/game/80896.html"><img src="/upload/otherpic58/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){
//插入广告
}
/*
thel3con = $('#thelist3').html();
alert(thel3con.length);
if(thel3con=""){
alert("空");
 
}else{
alert("非空");
}
*/
</script>

方法二、

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() === "" ){} 用三个等号更符合

看完了这篇文章,相信你对jquery怎么验证元素是否为空有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!

网页题目:jquery怎么验证元素是否为空
文章来源:https://www.cdcxhl.com/article40/johheo.html

成都网站建设公司_创新互联,为您提供域名注册网站维护动态网站微信公众号网站营销定制网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司