📅  最后修改于: 2020-11-26 01:11:04             🧑  作者: Mango
jQuery attr()方法用于设置或返回所选元素的属性和值。
jQuery attr()方法有两种用法。
句法:
要返回属性的值:
$(selector).attr(attribute)
设置属性和值:
$(selector).attr(attribute,value)
通过使用函数来设置属性和值:
$(selector).attr(attribute,function(index,currentvalue))
设置多个属性和值:
$(selector).attr({attribute:value, attribute:value,...})
Parameter | Description |
---|---|
Attribute | This parameter is used to specify the name of the attribute. |
Value | This parameter is used to specify the value of the attribute. |
Function (index, currentvalue) | It is a parameter to specify a function that returns an attribute value to set.
|
让我们以一个示例来演示jQuery attr()方法。
它具有两个主要优点: