jQuery API 返回首页目录 | jQuery API 中英文对照版
attr(properties)
attr(properties)

将一个“名/值”形式的对象设置为所有匹配元素的属性。 这是一种在所有匹配元素中批量设置很多属性的最佳方式。

返回值:jQuery

参数:

  • properties (Map): 作为属性的“名/值对”对象
 

示例:

为所有图像设置src和alt属性。

$("img").attr({ src: "test.jpg", alt: "Test Image" });

HTML 代码:

<img/>

结果:

<img src="test.jpg" alt="Test Image"/>

 

attr( properties )

Set a key/value object as properties to all matched elements.

This serves as the best way to set a large number of properties on all matched elements.

Return value: jQuery
Parameters:

  • properties (Map): Key/value pairs to set as object properties.
 

Example:

Sets src and alt attributes to all images.

 $("img").attr({ src: "test.jpg", alt: "Test Image" });  
Before:
 <img/>  
Result:
 <img src="test.jpg" alt="Test Image"/>  
相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog