返回值:String attr(name|properties|key,value|fn)
概述
設定或返回被選元素的屬性值。
參數
name String V1.0
屬性名稱
properties Map V1.0
作為屬性的「名/值對」對像
key,value String,Object V1.0
屬性名稱,屬性值
key,function(index, attr) String,Function V1.1
1:屬性名稱。
2:返回屬性值的函式,第一個參數為目前元素的索引值,第二個參數為原先的屬性值。
示例
參數name 描述:
返回文件中所有影象的src屬性值。
jQuery 程式碼:
$("img").attr("src");
參數properties 描述:
為所有影象設定src和alt屬性。
jQuery 程式碼:
$("img").attr({ src: "test.jpg", alt: "Test Image" });
參數key,value 描述:
為所有影象設定src屬性。
jQuery 程式碼:
$("img").attr("src","test.jpg");
參數key,回撥函式 描述:
把src屬性的值設定為title屬性的值。
jQuery 程式碼:
$("img").attr("title", function() { return this.src });