jQuery each() 方法

实例

提示每个 <li> 元素的文本:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
  5. <script>
  6. $(document).ready(function(){
  7. $("button").click(function(){
  8. $("li").each(function(){
  9. alert($(this).text())
  10. });
  11. });
  12. });
  13. </script>
  14. </head>
  15. <body>
  16. <button>提醒每个列表项的值</button>
  17. <ul>
  18. <li>Coffee</li>
  19. <li>Milk</li>
  20. <li>Soda</li>
  21. </ul>
  22. </body>
  23. </html>

定义与用法

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

提示return false 可用于提前停止循环。


语法

  1. $(selector).each(function(index,element))
参数描述
function(index,element)必填。为每个匹配元素运行的函数
  • index - 选择器的索引位置
  • element - 当前元素 (也可以使用 "this")

分类导航