屬性
[1] attr( name ) Returns: Object
說明: 獲取第一個匹配元素的屬性值。該方法使得獲取第一個匹配元素的屬性值變得非常容易。如果一個元素沒有設置相應的屬性,則該方法返回undefined,屬性包括:title, alt, src, href, width, style等等。
- var title = $("em").attr("title");
var title = $("em").attr("title");
name: String -- 用於獲取屬性值的屬性名。
[2] attr( properties ) Returns: jQuery
說明: 爲每個匹配的元素設置一組鍵/值對象的屬性(列表)。這是一種在所有匹配元素中批量設置很多屬性的最佳方式。 注意,如果你要設置對象的class屬性,你必須使用'className' 作爲屬性名。或者你可以直接使用.addClass( class ) 和 .removeClass( class ).
- $("img").attr({
- src: "/images/hat.gif",
- title: "jQuery",
- alt: "jQuery Logo"
- });
$("img").attr({ src: "/images/hat.gif", title: "jQuery", alt: "jQuery Logo" });
name: String -- 用於獲取屬性值的屬性名。