jQuery怎么检测某个元素是否存在?
大神,求带!
大神,求带! 2017-03-02 09:25:24
0
2
950

jQuery怎么检测某个元素是否存在?

大神,求带!
大神,求带!

Antworte allen(2)
数据分析师

jQuery怎么检测某个元素是否存在?-PHP中文网问答-jQuery怎么检测某个元素是否存在?-PHP中文网问答

围观一下哦,学习一下。

伊谢尔伦

使用$(selector)可以获得匹配元素集合,而匹配元素集合的length属性可以获得集合中匹配元素的个数,所以只要判断length属性值是否为零,就可以判断元素是否存在。代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文网</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js?1.1.9"></script>
<script type="text/javascript">
$(document).ready(function(){
 $("#check").click(function(){
  if($(".mytest").length>0){
   alert("此元素存在");
  }
 })  
})
</script>
<body>
<ul>
 <li>太阳出来了</li>
 <li>PHP中文网</li>
 <li class="mytest">p+css教程</li>
</ul>
<button id="check">点击检测</button>
</body>
</html>

如果集合中元素的数量大于零,那么元素存在,否则指定元素不存在。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!