jQuery prop () method
Examples
Add and remove a property named "color" of:
var $x = $("div");
$x.prop("color","FF0000");
$x.append("The color 属性: " + $x.prop("color"));
$x.removeProp("color");
});
try it"
Definition and Usage
prop () method sets or returns the selected element attributes and values.
When this method is used to return the property value, the first matching element is returned.
When this method is used to set the property value, for the set of matched elements set one or more attribute / value pairs.
Note: prop () method should be used to retrieve property values, such as DOM attributes (such as selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected).
Tip: To retrieve the HTML attributes, use the attr () instead of the method.
Tip: To remove property, use removeProp () method.
grammar
Returns the value of the property:
The properties and values:
Use the function to set the properties and values:
Set multiple properties and values:
参数 | 描述 |
---|---|
property | 规定属性的名称。 |
value | 规定属性的值。 |
function(index,currentvalue) | 规定返回要设置的属性值的函数。
|
More examples
Different prop () and attr () between
prop () and attr () may return different values. This example demonstrates that when the time for returning the check box "checked" state is different.