Home  >  Article  >  Web Front-end  >  How to use the prop() method in jquery

How to use the prop() method in jquery

王林
王林Original
2020-11-24 11:01:474112browse

Usage of the prop() method in jquery: If you want to return the value of the property, use the method [$(selector).prop(property)]. If you want to set the property and value, use the method [$(selector) ).prop(property,value)].

How to use the prop() method in jquery

The operating environment of this tutorial: windows10 system, jquery2.2.4 version. This method is suitable for all brands of computers.

Usage of the prop() method in jquery:

prop()The method sets or returns the attributes and values ​​of the selected element.

(Learning video sharing: javascript video tutorial)

When this method is used to return an attribute value, the value of the first matching element is returned.

When this method is used to set attribute values, one or more attribute/value pairs are set for the set of matching elements.

Syntax:

Return the value of the attribute:

$(selector).prop(property)

Set the attribute and value:

$(selector).prop(property,value)

Use the function to set the attribute and value:

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

Set multiple attributes and values:

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

Attribute value:

  • property Specifies the name of the attribute.

  • #value Specifies the value of the attribute.

  • function(index,currentvalue) Specifies the function that returns the attribute value to be set.

index - Retrieve the index position of the element in the collection.

CurrentValue-Search the current attribute value of the selected element.

Example:

Add and remove an attribute named "color":

$("button").click(function(){
    var $x = $("div");
    $x.prop("color","FF0000");
    $x.append("The color 属性: " + $x.prop("color"));
    $x.removeProp("color");});

Related recommendations:js tutorial

The above is the detailed content of How to use the prop() method in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn