Home  >  Article  >  Web Front-end  >  How to verify if an element is empty in jquery

How to verify if an element is empty in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-18 14:55:082182browse

Jquery method to verify whether an element is empty: 1. Use [val()] to judge input, and the code is [var value = $('#test').val()]; 2. Use [html ()] Judgment, the code is [var value = $('#test').html()].

How to verify if an element is empty in jquery

The operating environment of this tutorial: windows10, jquery2.2.4, this article is applicable to all brands of computers.

Jquery method to verify whether an element is empty:

Input uses val();

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

Whether it is empty Judgment method:

  • if(value.length == 0){} Operation performed if value is empty

  • if(value!= ''){} If the value is not empty, the operation performed

html element is judged by html();

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

is empty. Method:

if(value.length == 0){

You can also judge whether there are 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() === "" ){} Using three equal signs is more in line with

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to verify if an element 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