发布于:使用 jQuery 核心

属性

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

link .attr() 方法

.attr() 方法既是获取器也是设置器。作为设置器,.attr() 可以接受一个键和一个值,或者一个包含一个或多个键值对的对象。

作为设置器的 .attr()

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

作为获取器的 .attr()

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