返回值:Stringattr(name|properties|key,value|fn)
概述
設置或返回被選元素的屬性值。
參數
nameStringV1.0
屬性名稱
properties MapV1.0
作為屬性的“名/值對”對象
key,value String,ObjectV1.0
屬性名稱,屬性值
key,function(index, attr) String,FunctionV1.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 });