您好,欢迎访问一九零五行业门户网

jquery 学习之二 属性相关_jquery

attr(name)
取得第一个匹配元素的属性值。通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则返回 undefined 。
access a property on the first matched element. this method makes it easy to retrieve a property value from the first matched element. if the element does not have an attribute with such a name, undefined is returned.
返回值
object
参数
name (string) : 属性名称
示例
返回文档中第一个图像的src属性值。
html 代码:
jquery 代码:
$(img).attr(src);
结果:
test.jpg
---------------------------------------------------------------------------------------------------------------------------------------
attr(properties)
将一个“名/值”形式的对象设置为所有匹配元素的属性。
这是一种在所有匹配元素中批量设置很多属性的最佳方式。 注意,如果你要设置对象的class属性,你必须使用'classname' 作为属性名。或者你可以直接使用.addclass( class ) 和 .removeclass( class ).
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. note that you must use 'classname' as key if you want to set the class-attribute. or use .addclass( class ) or .removeclass( class ).
返回值
jquery
参数
properties (map) : 作为属性的“名/值对”对象
示例
为所有图像设置src和alt属性。
html 代码:
jquery 代码:
$(img).attr({ src: test.jpg, alt: test image });
结果:
[ ]
---------------------------------------------------------------------------------------------------------------------------------------
attr(key,value)
为所有匹配的元素设置一个属性值。
set a single property to a value, on all matched elements.
返回值
jquery
参数
key (string) : 属性名称
value (object) : 属性值
示例
为所有图像设置src属性。
html 代码:
jquery 代码:
$(img).attr(src,test.jpg);
结果:
[ , ]
---------------------------------------------------------------------------------------------------------------------------------------
attr(key,fn)
为所有匹配的元素设置一个计算的属性值。
不提供值,而是提供一个函数,由这个函数计算的值作为属性值。
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.
返回值
jquery
参数
key (string) : 属性名称
fn (function) : 返回值的函数 范围:当前元素, 参数: 当前元素的索引值
示例
把src属性的值设置为title属性的值。
html 代码:
jquery 代码:
$(img).attr(title, function() { return this.src });
结果:
---------------------------------------------------------------------------------------------------------------------------------------
removeattr(name)
从每一个匹配的元素中删除一个属性
remove an attribute from each of the matched elements.
返回值
jquery
参数
name (string) : 要删除的属性名
示例
将文档中图像的src属性删除
html 代码:
jquery 代码:
$(img).removeattr(src);
结果:
[ ]
其它类似信息

推荐信息