元素的属性能够包含应用程序中很有用的信息,因此能够获取和设置属性是非常重要的。
.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