+ -
当前位置:首页 → 问答吧 → find 与 children 有什么区别???

find 与 children 有什么区别???

时间:2010-02-08

来源:互联网

感觉是一样的,一般都是用find吧?

作者: 115300111   发布时间: 2010-02-08

The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.
文档说的很清楚的么,这个2个方法很相似,但是后者只遍历1层dom树。
比如
复制代码
  1. <div id="level1" class="haha">
  2. <div id="level2" class='haha'>
  3. <div id="level3" class="haha"></div>
  4. </div>
  5. </div>

这样的,你用$("#level1").find('.haha')和$("#level1").children('.haha')看看结果就知道了
附上完整代码及结果:
复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title></title>
  6. <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js" ></script>
  7. </head>
  8. <body>
  9. <div id="level1" class="haha">
  10. <div id="level2" class='haha'>
  11. <div id="level3" class="haha"></div>
  12. </div>
  13. </div>
  14. <script type="text/javascript">
  15.     //<![CDATA[
  16.         console.log($("#level1").find('.haha'));
  17.         console.log($("#level1").children('.haha'));
  18.     //]]>
  19. </script>
  20. </body>
  21. </html>


作者: ywqbestever   发布时间: 2010-02-08

相关阅读 更多