溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

jquery怎么驗(yàn)證元素是否為空

發(fā)布時(shí)間:2020-11-19 10:00:24 來(lái)源:億速云 閱讀:177 作者:小新 欄目:web開發(fā)

小編給大家分享一下jquery怎么驗(yàn)證元素是否為空,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

jquery驗(yàn)證元素是否為空的方法:1、input用【val()】判斷,代碼為【var value = $('#test').val()】;2、用【html()】判斷,代碼為【var value = $('#test').html()】。

jquery驗(yàn)證元素是否為空的方法:

input 用val();

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

是否為空的判斷方法:

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

  • if(value!=''){} 如果value不為空?qǐng)?zhí)行的操作

html元素用html();

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

是否為空的判斷方法:

if(value.length == 0){

也可以通過(guò)判斷是說(shuō)有沒(méi)有子節(jié)點(diǎn)?對(duì)于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="//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">海島奇兵國(guó)服內(nèi)購(gòu)破解版</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>

方法二、

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() === "" ){} 用三個(gè)等號(hào)更符合

看完了這篇文章,相信你對(duì)jquery怎么驗(yàn)證元素是否為空有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI