Home  >  Article  >  Web Front-end  >  How to determine whether the element content is empty in jquery

How to determine whether the element content is empty in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-24 15:28:232654browse

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.

How to determine whether the element content is empty in jquery

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();

How to judge whether it is empty:

  • 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();

How to judge whether it is empty:

if(value.length == 0){

You can also judge whether there are any child nodes? For html acquisition methods, you can use

$('#list').children().length === 0

Method 1

<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($(&#39;#thelist2&#39;).children().length)
alert($(&#39;#thelist3&#39;).children().length)
$thelist3 = $(&#39;#thelist3&#39;);
if($thelist3.children().length==0){
//插入广告
}
/*
thel3con = $(&#39;#thelist3&#39;).html();
alert(thel3con.length);
if(thel3con=""){
alert("空");
 
}else{
alert("非空");
}
*/
</script>

Method 2.

String.prototype.isEmpty = function () {
 var s1 = this.replace(/[\r\n]/g, &#39;&#39;).replace(/[ ]/g, &#39;&#39;),
   s2 = (s1 == &#39;&#39;) ? 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn