jQuery 遍历 - each() 方法

定义和用法

each() 方法规定为每个匹配元素规定运行的函数。

提示:返回 false 可用于及早停止循环。

语法

$(selector).each(function(index,element))
参数 描述
function(index,element)

必需。为每个匹配元素规定运行的函数。

  • index - 选择器的 index 位置
  • element - 当前的元素(也可使用 "this" 选择器)
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("li").each(function(i,e){
      alert($(e).text()+i)
    });
  });
});
</script>
</head>
<body>
<button>输出每个列表项的值</button>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>
</html>

猜你喜欢

转载自4636.iteye.com/blog/2329015