日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jquery判断元素内容是否为空的方法

發(fā)布時間:2024/1/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jquery判断元素内容是否为空的方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

用jquery獲取元素內(nèi)容需要分兩個情況:

input 用val();

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

是否為空的判斷方法:

if(value.length?== 0){} 如果value為空執(zhí)行的操作

if(value!=''){} 如果value不為空執(zhí)行的操作

jQuery驗證文本框內(nèi)容不為空

通過$.fn 擴展jQuery方法

1

2

3

4

5

6

7

8

9

10

11

12

13

/**

?* 校驗文本是否為空

?* tips:提示信息

?* 使用方法:$("#id").validate("提示文本");

?* @itmyhome

?*/

$.fn.validate = function(tips){

?

??if($(this).val() == "" || $.trim($(this).val()).length == 0){

????alert(tips + "不能為空!");

????throw SyntaxError(); //如果驗證不通過,則不執(zhí)行后面

??}

}

html元素用html();

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

是否為空的判斷方法:

if(value.length == 0){

?也可以通過判斷是說有沒有子節(jié)點?對于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"> 節(jié)奏大師全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">海島奇兵國服內(nèi)購破解版</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() === "" ){} 用三個等號更符合

總結(jié)

以上是生活随笔為你收集整理的jquery判断元素内容是否为空的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。