1. 程式人生 > >jQuery中的attr()方法和each()方法

jQuery中的attr()方法和each()方法

一、attr()

返回被選元素的屬性值。

$(selector).attr(attribute)
引數 描述
attribute 規定要獲取其值的屬性。

設定被選元素的屬性和值。

$(selector).attr(attribute,value)
引數 描述
attribute 規定屬性的名稱。
value 規定屬性的值。

二、each()

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

提示:返回 false 可用於及早停止迴圈。

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

必需。為每個匹配元素規定執行的函式。

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