Home > Web Front-end > JS Tutorial > How to determine if the element content is empty in jquery

How to determine if the element content is empty in jquery

php中世界最好的语言
Release: 2018-06-01 11:28:57
Original
2215 people have browsed it

This time I will bring you jqueryHow to determine whether the element content is empty, jquery determines whether the element content is emptyWhat are the precautions, the following is a practical case, let's take a look .

Using jquery to obtain element content requires two situations:

Input uses val();

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

How to determine whether it is empty:

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

if (value!=''){} Operations performed if value is not empty

jQuery verifies that the content of the text box is not empty

Extended jQuery method through $.fn

/**
 * 校验文本是否为空
 * tips:提示信息
 * 使用方法:$("#id").validate("提示文本");
 * @itmyhome
 */
$.fn.validate = function(tips){
  if($(this).val() == "" || $.trim($(this).val()).length == 0){
    alert(tips + "不能为空!");
    throw SyntaxError(); //如果验证不通过,则不执行后面
  }
}
Copy after login

html elementUse 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 the method of html acquisition,

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

Method 1

<script type="text/javascript" src="http://m.jb51.net/skin/mobile/js/jquery.min.js"></script>
<p><ul id="thelist2">
<li><a href="https://m.jb51.net/game/140209.html"><img src="//files.jb51.net/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="//files.jb51.net/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="//files.jb51.net/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="//files.jb51.net/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="//files.jb51.net/do/uploads/litimg/130503/1J21Va46.jpg">完美女友完整版</a><em class="xj star5"></em></li>
</ul>
<p><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>
Copy after login

Method 2,

String.prototype.isEmpty = function () {
 var s1 = this.replace(/[\r\n]/g, '').replace(/[ ]/g, ''),
   s2 = (s1 == '') ? true : false;
 return s2;
};
$list.html().isEmpty();
Copy after login

if( $("#list").html() === "" ){} Use three etc. The number is more in line with

. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use Vue to implement a floating/hidden menu in the upper right corner of the page

How to use vue mint -ui tabbar (with code)

The above is the detailed content of How to determine if the element content is empty in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template