84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
jQuery怎么检测某个元素是否存在?
How does jQuery detect whether an element exists? -PHP Chinese website Q&A-How does jQuery detect whether an element exists? -PHP Chinese website Q&A
Let’s take a look and learn.
使用$(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>
如果集合中元素的数量大于零,那么元素存在,否则指定元素不存在。
How does jQuery detect whether an element exists? -PHP Chinese website Q&A-How does jQuery detect whether an element exists? -PHP Chinese website Q&A
Let’s take a look and learn.
使用$(selector)可以获得匹配元素集合,而匹配元素集合的length属性可以获得集合中匹配元素的个数,所以只要判断length属性值是否为零,就可以判断元素是否存在。代码实例如下:
如果集合中元素的数量大于零,那么元素存在,否则指定元素不存在。