Jquery method to determine whether the element content is empty: 1. Use the [if(value.length == 0){}] method to perform the operation if the value is empty; 2. Use [if(value! =''){}] method, the operation performed if value is not empty.
The operating environment of this tutorial: windows7 system, jquery3.2.1 version. This method is suitable for all brands of computers.
Jquery method to determine whether the element content is empty:
input uses val();
##var value = $('#test').val();
if(value.length == 0){}Operations performed if value is empty
if(value!=''){}Executed if value is not empty Operation
jQuery validates that the text box content is not empty
Extended jQuery method by$.fn
/** * 校验文本是否为空 * tips:提示信息 * 使用方法:$("#id").validate("提示文本"); * @itmyhome */ $.fn.validate = function(tips){ if($(this).val() == "" || $.trim($(this).val()).length == 0){ alert(tips + "不能为空!"); throw SyntaxError(); //如果验证不通过,则不执行后面 } }
html element uses html();
var value = $('#test').html();
if(value.length == 0){
$('#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"> 节奏大师全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(); if( $("#list").html() === "" ){} 用三个等号更符合
Related free learning recommendations:JavaScript (video)
The above is the detailed content of How to determine whether the element content is empty in jquery. For more information, please follow other related articles on the PHP Chinese website!