jQuery 遍歷 - each() 方法

實例

輸出每個 li 元素的文本:

$("button").click(function(){
  $("li").each(function(){
    alert($(this).text())
  });
});

親自試一試

定義和用法

each() 方法規定為每個匹配元素規定運行的函數。

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

語法

$(selector).each(function(index,element))
參數 描述
function(index,element)

必需。為每個匹配元素規定運行的函數。

  • index - 選擇器的 index 位置
  • element - 當前的元素(也可使用 "this" 選擇器)