AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > PHP编程

jQuery遍历节点-find()方法使用说明

51自学网 2016-09-19 http://www.wanshiok.com
find()方法在jquery是匹配元素的后代元素中按照选择器表达式进行筛选,它可以根据传入的参数进入筛选遍历,下面我来给各位同学详细介绍。

find(selector)

此方法用于在匹配元素的后代元素中按照选择器表达式进行筛选。
记住:使用此方法必须得传入选择器表达式参数,不然是获取不到任何元素的,也就失去了使用此方法的意义了。

我最近才想明白,利用jquery方法如何找到一个元素中的所有的后代元素。使用这个.find()方法就可以轻松地做到。
来看例子:
 

<div id="level_one">     我是最外一层的div纯文本内容     <div>         我是第二层div的纯文本内容         <span>jquery基础教程</span>         <span class="item">jquery教程</span>     </div>     <div>         我也是第二层div的纯文本内容         <span class="item">PHP学习</span>         <span>PHP教程</span>     </div> </div>$("#level_one").find("*").length;//这个就是获取id为“level_one”的div中的所有的元素的个数,结果为6。 $("#level_one").find("div").length;//会获取到2个元素 $("#level_one").find("span").length;//会获取到4个元素 $("#level_one").find("span.item").length;//会获取到2个元素

<style>#level_one{width:240px;height:360px;border:2px solid #0000FF;padding:10px;float:left}#level_one div{width:200px;height:150px;border:1px solid #FF0000;margin:10px;}#level_one div span{float:left;width:150px;height:30px;border:1px solid #999000;margin:10px;}</style><div id="level_one"> 我是最外一层的div纯文本内容 <div>  我是第二层div的纯文本内容  <span>jquery基础教程</span>  <span>jquery教程</span> </div> <div>  我也是第二层div的纯文本内容  <span>PHP学习</span>  <span>PHP教程</span> </div></div><input type="button" id="test1" value="获取div#level_one的所有后代元素"><input type="button" id="test2" value="获取div#level_one的中的span"><input type="button" id="test3" value="获取div#level_one的中的div"><script>$(function(){ $("#test1").click(function(){  alert($("#level_one").find("*").length); }); $("#test2").click(function(){  alert($("#level_one").find("span").length); }); $("#test3").click(function(){  alert($("#level_one").find("div").length); });})</script>

其实上面的jquery代码和下面的jquery代码的效果是一样的。
$("#level_one *").length;//会获取到6个元素 $("#level_one div").length;//会获取到2个元素 $("#level_one span").length;//会获取到4个元素 $("#level_one span.item").length;//会获取到2个元素

<style>#level_one{width:240px;height:360px;border:2px solid #0000FF;padding:10px;float:left}#level_one div{width:200px;height:150px;border:1px solid #FF0000;margin:10px;}#level_one div span{float:left;width:150px;height:30px;border:1px solid #999000;margin:10px;}</style><div id="level_one"> 我是最外一层的div纯文本内容 <div>  我是第二层div的纯文本内容  <span>jquery基础教程</span>  <span class="item">jquery教程</span> </div> <div>  我也是第二层div的纯文本内容  <span class="item">PHP学习</span>  <span>PHP教程</span> </div></div><font color="#FF0000">点击下面的button都是在id为level_one的div中筛选元素</font><br><input type="button" id="test1" value="获取所有后代元素的个数"><br><input type="button" id="test2" value="获取span的个数"><br><input type="button" id="test3" value="获取div的个数"><br><input type="button" id="test4" value="获取class为item的span的个数"><br><script>$(function(){ $("#test1").click(function(){  alert($("#level_one *").length); }); $("#test2").click(function(){  alert($("#level_one span").length); }); $("#test3").click(function(){  alert($("#level_one div").length); }); $("#test4").click(function(){  alert($("#level_one span.item").length); });})</script>



说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
 
上一篇:jquery.validate.js和jquery.metadata.js的例子  下一篇:jQuery插件ajaxfileupload.js实现ajax上传文件