如何检查每个函数内的长度

我有麻烦检查$.each()函数内的length

假设我有2个这样的elements

 <div class="demo"> <!-- 30 -- with different class> <input type="text" name="demo" class="hell"/> <input type="text" name="demo2" class="seeHere"/> </div> 
 $('.demo').find('input').each(function(){ var $this = $(this); if($this.find('.seeHere').length>0){ // throws error length of undifined // do something }else{ // do something } }); 

它会抛出if的错误长度为undifined

至于第一次它将chache

  <input type="text" name="demo" class="hell"/> 

有没有什么办法可以返回false if(el.length>0 || return false) like el.push(value || 'No value')

请提前帮助我

使用jQuery的hasClass

 $('.demo').find('input').each(function(){ var $this = $(this); if($this.hasClass('seeHere')){ // do something }else{ // do something } }); 

在jQuery的每个函数中,$(this)类似于索引处该select器的元素

我的意思是说

假设你有这样的事情

 <div id="#a" class="abc"> <div id="#b" class="abc"> <div id="#c" class="abc"> 

所以当你循环“.abc”就好了

 $(".abc").each(function() { //Here $(this) resembles to $("#a") at first iteration // $("#b") at second iteration // $("#c") at third iteration }); 

您不需要遍历任何东西 – 使用所需的select器和.length属性来获取该类的元素的数量..

 $(document).ready(function(){ var len = $('.seeHere').length; console.log(len); //displays 2 in the console since there are two divs with the desired class }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="a" class="hell"> <div id="b" class="seeHere"> <div id="c" class="something"> <div id="d" class="seeHere">