jQuery API | jQuery API 中英文对照版 - jQuery在线查询手册 |
attr(key,fn) |
attr(key, fn) 为所有匹配的元素设置一个计算的属性值。不提供值,而是提供一个函数,由这个函数计算的值作为属性值。 返回值:jQuery 参数:
[示例]: 把src属性的值设置为title属性的值。 $("img").attr("title", function() { return this.src });HTML标记: <img src="test.jpg" />结果: <img src="test.jpg" title="test.jpg" /> Set a single property to a computed value, on all matched elements. Instead of supplying a string value as described above, a function is provided that computes the value. Return value: jQuery
Example: Sets title attribute from src attribute. $("img").attr("title", function() { return this.src });Before: <img src="test.jpg" />Result: <img src="test.jpg" title="test.jpg" /> Example: Enumerate title attribute. $("img").attr("title", function(index) { return this.title + (++index); });Before: <img title="pic" /><img title="pic" /><img title="pic" />Result: <img title="pic1" /><img title="pic2" /><img title="pic3" />
|