jquery如何验证元素是否为空


本文摘自PHP中文网,作者coldplay.xixi,侵删。

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

本教程操作环境:windows10、jquery2.2.4,本文适用于所有品牌的电脑。

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

input 用val();

1

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

是否为空的判断方法:

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

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

html元素用html();

1

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

是否为空的判断方法:

1

if(value.length == 0){

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

1

$('#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){

//插入广告

}

/*

thel3con = $('#thelist3').html();

alert(thel3con.length);

if(thel3con=""){

alert("空");

  

}else{

alert("非空");

}

*/

</script>

方法二、

1

2

3

4

5

6

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怎么获取元素的坐标?

jQuery怎么去除css属性

jQuery中on()与click()的区别是什么?

jQuery如何改变class属性

jQuery发请求传输中文参数乱码怎么办

jQuery file是什么意思

jQuery选择器有什么优点?

jQuery css()和attr()的区别是什么

jQuery如何设置按钮不可点击

jQuery中smart ui是什么

更多相关阅读请进入《jQuery》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...