jquery如何驗證元素是否為空
jquery
是否為空
jquery驗證元素是否為空的方法:1、input用【val()】判斷,程式碼為【var value = $('#test').val()】;2、用【html ()】判斷,程式碼為【var value = $('#test').html()】。
本教學操作環境:windows10、jquery2.2.4,本文適用於所有品牌的電腦。
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){
登入後複製
是否為空的判斷方法:
$('#list').children().length === 0
登入後複製
也可以透過判斷是說有沒有子節點?對於html取得的方法可用
<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>
登入後複製
方法一
String.prototype.isEmpty = function () { var s1 = this.replace(/[\r\n]/g, '').replace(/[ ]/g, ''), s2 = (s1 == '') ? true : false; return s2; }; $list.html().isEmpty();
登入後複製
方法二、相關免費學習推薦:###JavaScript###(影片)#######
以上是jquery如何驗證元素是否為空的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)