>  기사  >  웹 프론트엔드  >  jquery에서 요소 내용이 비어 있는지 확인하는 방법

jquery에서 요소 내용이 비어 있는지 확인하는 방법

coldplay.xixi
coldplay.xixi원래의
2020-11-24 15:28:232576검색

jquery가 요소 콘텐츠가 비어 있는지 확인하는 방법: 1. 값이 비어 있는 경우 [if(value.length == 0){}] 메서드를 사용하여 작업을 수행합니다. 2. [if(value!=')를 사용합니다. ') {}] 메서드, 값이 비어 있지 않으면 수행되는 작업입니다.

jquery에서 요소 내용이 비어 있는지 확인하는 방법

이 튜토리얼의 운영 환경: windows7 시스템, jquery3.2.1 버전 이 방법은 모든 브랜드의 컴퓨터에 적합합니다.

요소 내용이 비어 있는지 확인하는 Jquery 메서드:

input use val();

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

是否为空的判断方法:

  • if(value.length == 0){}如果value为空执行的操作

  • if(value!=''){}如果value不为空执行的操作

jQuery验证文本框内容不为空

通过$.fn扩展jQuery方法

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

html元素用html();

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

是否为空的判断方法:

if(value.length == 0){

 也可以通过判断是说有没有子节点?对于html获取的方法可用

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

비어 있는지 판단하는 방법:

  • if(value.length == 0){}값이 다음과 같은 경우 비어 있으면 작업 실행

  • if(value!=''){}값이 비어 있지 않으면 수행되는 작업

jQuery 유효성 검사 text 상자의 내용이 비어 있지 않습니다

html();🎜🎜🎜var 값 = $('#)이 있는 <code>$.fn🎜
<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>
🎜🎜html 요소를 통해 확장된 jQuery 메서드 test').html();🎜🎜이 비어 있는지 판단하는 방법: 🎜🎜if(value.length == 0){🎜🎜 비어 있는지 판단할 수도 있습니다. 자식 노드? HTML 획득 방법의 경우 🎜🎜$('#list').children().length === 0 🎜🎜Method 1🎜
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() === "" ){} 用三个等号更符合
🎜Method 2, 🎜rrreee🎜🎜을 사용할 수 있습니다. 관련 무료 학습 추천 :🎜JavaScript🎜(동영상)🎜🎜

위 내용은 jquery에서 요소 내용이 비어 있는지 확인하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.