Jquery中的$().each() 方法

先举例子,

输出每个 li 元素的文本:

 1 <html>
 2 <head>
 3 <script type="text/javascript" src="/jquery/jquery.js"></script>
 4 </head>
 5 <body>
 6 <button>输出每个列表项的值</button>
 7 <ul>
 8 <li>Coffee</li>
 9 <li>Milk</li>
10 <li>Soda</li>
11 </ul>
12 </body>
13 <script type="text/javascript">
14 $(document).ready(function(){
15   $("button").click(function(){
16     $("li").each(function(){
17       alert($(this).text())
18     });
19   });
20 });
21 </script>
22 
23 </html>
View Code

语法:
  $(selector).each(function(index,element))

说明: function(index,element)

其中function函数为是必需的,它为每一个匹配元素规定了运行的函数

index为选择器的位置,element指向当前的元素(也可以使用“this”选择器)。

 

posted @ 2015-11-06 10:05  Seekload  阅读(224)  评论(0编辑  收藏  举报