jquery如何判断指定子元素是否存在(jquery,web开发)

时间:2024-04-28 04:12:08 作者 : 石家庄SEO 分类 : web开发
  • TAG :

判断方法:1、使用“$("父元素").has("子元素").length”语句,如果返回值为1,则指定子元素存在;2、使用“$("父元素").children("子元素").length”语句,如果返回值大于等于1,则指定子元素存在。

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。

jquery判断指定子元素是否存在

方法1:利用has() 方法

has() 将匹配元素集合缩减为拥有匹配指定选择器或 DOM 元素的后代的子集。

<!DOCTYPEhtml><html> <head> <scripttype="text/javascript"src="js/jquery-1.10.2.min.js"></script> <scripttype="text/javascript"> $(document).ready(function(){ $("button").click(function(){ if($("div").has("span").length){ console.log("指定子元素存在") }else{ console.log("指定子元素不存在") } }); }); </script> </head> <body> <divstyle="border:1pxsolidred;"> <p>子元素1</p> <span>子元素2</span> </div><br> <button>指定子元素span是否存在</button> </body></html>

jquery如何判断指定子元素是否存在

方法2:使用children()

children() 方法返回返回被选元素的所有直接子元素。

<!DOCTYPEhtml><html> <head> <scripttype="text/javascript"src="js/jquery-1.10.2.min.js"></script> <scripttype="text/javascript"> $(document).ready(function(){ $("button").click(function(){ if($("div").children("p").length){ console.log("指定子元素存在"); console.log($("div").children("p").length); }else{ console.log("指定子元素不存在"); console.log($("div").children("p").length); } }); }); </script> </head> <body> <divstyle="border:1pxsolidred;"> <p>子元素1</p> <span>子元素2</span> <span>子元素2</span> <p>子元素3</p> <p>子元素4</p> </div><br> <button>指定子元素是否存在</button> </body></html>

jquery如何判断指定子元素是否存在

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:jquery如何判断指定子元素是否存在的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:怎么用纯CSS实现抛盒子的loader下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18