发布在:使用 jQuery Core

属性

元素的属性可能包含对你的应用程序有用的信息,因此能够获取和设置它们非常重要。

链接 .attr() 方法

.attr() 方法既可以作为 getter,也可以作为 setter。作为 setter,.attr() 可以接受一个键和一个值,或一个包含一个或多个键/值对的对象。

.attr() 作为 setter

1
2
3
4
5
6
$( "a" ).attr( "href", "allMyHrefsAreTheSameNow.html" );
$( "a" ).attr({
title: "all titles are the same too!",
href: "somethingNew.html"
});

.attr() 作为 getter

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