參數
name (String) : 屬性名稱
示例
返回文檔中第一個圖像的src屬性值。
HTML 代碼:
jQuery 代碼:
$("img").attr("src");
結果:
https://www.gxlcms.com/test.jpg
---------------------------------------------------------------------------------------------------------------------------------------
attr(properties)
將一個“名/值”形式的對象設置為所有匹配元素的屬性。
這是一種在所有匹配元素中批量設置很多屬性的最佳方式。 注意,如果你要設置對象的class屬性,你必須使用'className' 作為屬性名?;蛘吣憧梢灾苯邮褂?addClass( class ) 和 .removeClass( class ).
Set a key/value object as properties to all matched elements.
This serves as the best way to set a large number of properties on all matched elements. Note that you must use 'className' as key if you want to set the class-Attribute. Or use .addClass( class ) or .removeClass( class ).
返回值
jQuery
參數
properties (Map) : 作為屬性的“名/值對”對象
示例
為所有圖像設置src和alt屬性。
HTML 代碼:
jQuery 代碼:
$("img").attr({ src: "https://www.gxlcms.com/test.jpg", alt: "Test Image" });
結果:
[ ]
---------------------------------------------------------------------------------------------------------------------------------------
attr(key,value)
為所有匹配的元素設置一個屬性值。
Set a single property to a value, on all matched elements.
返回值
jQuery
參數
key (String) : 屬性名稱
value (Object) : 屬性值
示例
為所有圖像設置src屬性。
HTML 代碼:
jQuery 代碼:
$("img").attr("src","https://www.gxlcms.com/test.jpg");
結果:
[ ,
]
---------------------------------------------------------------------------------------------------------------------------------------
attr(key,fn)
為所有匹配的元素設置一個計算的屬性值。
不提供值,而是提供一個函數,由這個函數計算的值作為屬性值。
Set a single property to a computed value, on all matched elements.
Instead of supplying a string value as described 'above', a function is provided that computes the value.
返回值
jQuery
參數
key (String) : 屬性名稱
fn (Function) : 返回值的函數 范圍:當前元素, 參數: 當前元素的索引值
示例
把src屬性的值設置為title屬性的值。
HTML 代碼:
jQuery 代碼:
$("img").attr("title", function() { return this.src });
結果:
---------------------------------------------------------------------------------------------------------------------------------------
removeAttr(name)
從每一個匹配的元素中刪除一個屬性
Remove an attribute from each of the matched elements.
返回值
jQuery
參數
name (String) : 要刪除的屬性名
示例
將文檔中圖像的src屬性刪除
HTML 代碼:
jQuery 代碼:
$("img").removeAttr("src");
結果:
[ ]
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com