Home > Web Front-end > JS Tutorial > body text

How to use the prop() method in jquery

王林
Release: 2020-11-30 14:56:57
Original
4195 people have browsed it

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)
Copy after login

Set the attribute and value:

$(selector).prop(property,value)
Copy after login

Use the function to set the attribute and value:

$(selector).prop(property,function(index,currentvalue))
Copy after login

Set multiple attributes and values:

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

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");});
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template