天天看點

jQuery入門:屬性(Attributes)

元素的屬性能夠包含應用程式中很有用的資訊,是以能夠擷取和設定屬性是非常重要的。

.attr()

方法

.attr()

方法既可以做擷取函數又可以做設定函數。如果是設定函數,

.attr()

能夠接受鍵名和鍵值或者包含一對或更多鍵/值對的對象。

.attr()

用來做設定函數:

$( "a" ).attr( "href", "allMyHrefsAreTheSameNow.html" );

$( "a" ).attr({
    title: "all titles are the same too!",
    href: "somethingNew.html"
});
           

.attr()

用來做擷取函數:

$( "a" ).attr( "href" ); // Returns the href for the first a element in the document
           

原文位址