六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 20|回复: 0

jquery 遍历 children([expr])

[复制链接]

升级  67.33%

130

主题

130

主题

130

主题

举人

Rank: 3Rank: 3

积分
402
 楼主| 发表于 2013-2-7 16:02:25 | 显示全部楼层 |阅读模式
children([expr])
从当前匹配元素集合中得到各个元素第一层子集集合,形成新的元素集合。返回匹配元素集合
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                     "http://www.w3.org/TR/html4/loose.dtd"><html><head>  <script src="http://code.jquery.com/jquery-latest.js"></script>    <script>  $(document).ready(function(){        $("#container").click(function (e) {      $("*").removeClass("hilite");      var $kids = $(e.target).children();      var len = $kids.addClass("hilite").length;      $("#results span:first").text(len);      $("#results span:last").text(e.target.tagName);      e.preventDefault();      return false;    });  });  </script>  <style>  body { font-size:16px; font-weight:bolder; }  div { width:130px; height:82px; margin:10px; float:left;        border:1px solid blue; padding:4px; }  #container { width:auto; height:105px; margin:0; float:none;        border:none; }  .hilite { border-color:red; }  #results { display:block; color:red; }  p { margin:10px; border:1px solid transparent; }  span { color:blue; border:1px solid transparent; }  input { width:100px; }  em { border:1px solid transparent; }  a { border:1px solid transparent; }  b { border:1px solid transparent; }  button { border:1px solid transparent; }  </style></head><body>  <div id="container">    <div>      <p>This <span>is the <em>way</em> we</span>       write <em>the</em> demo,</p>    </div>    <div>      <a href="#"><b>w</b>rit<b>e</b></a> the <span>demo,</span> <button>write       the</button> demo,    </div>    <div>      This <span>the way we <em>write</em> the <em>demo</em> so</span>      <input type="text" value="early" /> in    </div>    <p>      <span>t</span>he <span>m</span>orning.      <span id="results">Found <span>0</span> children in <span>TAG</span>.</span>    </p>  </div></body></html> 
var $kids = $(e.target).children();
将得到点击后匹配元素集合中第一层子集元素集合。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表