Latest web development tutorials

jQuery prop () method

jQuery HTML / CSS Methods jQuery HTML / CSS Methods

Examples

Add and remove a property named "color" of:

$("button").click(function(){
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:

$(selector).prop(property)

The properties and values:

$(selector).prop(property,value)

Use the function to set the properties and values:

$(selector).prop(property,function(index,currentvalue ))

Set multiple properties and values:

$(selector).prop({property:value,property:value,...})

参数 描述
property 规定属性的名称。
value 规定属性的值。
function(index,currentvalue) 规定返回要设置的属性值的函数。
  • index- 检索集合中元素的 index 位置。
  • currentvalue- 检索被选元素的当前属性值。


Examples

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.


jQuery HTML / CSS Methods jQuery HTML / CSS Methods