What are the commonly used methods to obtain attributes in jquery?

青灯夜游
Release: 2022-03-23 17:24:03
Original
12440 people have browsed it

The commonly used methods for obtaining attributes in jquery are: 1. attr() method, which can obtain and return the value of the specified attribute. The syntax is "$(selector).attr("attribute name")"; 2. prop( ) method, which can return the attribute value of the selected element, the syntax is "$(selector).prop("property name")".

What are the commonly used methods to obtain attributes in jquery?

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

There are two commonly used methods for obtaining attributes in jquery:

  • attr() method

  • prop() method

jquery attr() method

attr() method sets or returns the attribute value of the selected element.

Example: Get the value of the width attribute of the img element

      
What are the commonly used methods to obtain attributes in jquery?
Copy after login

What are the commonly used methods to obtain attributes in jquery?

##jquery prop() method## The #prop() method sets or returns the properties and values of the selected element.

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

Example:

      
What are the commonly used methods to obtain attributes in jquery?
Copy after login

The difference between attr() and prop() methods


prop() method and attr( ) methods are similar, both are used to get or set the HTML attributes of elements, but there are essential differences between the two.

jQuery official recommendation: For attributes with two values of true and false, such as checked, selected, disabled, etc., it is recommended to use the prop() method to operate, while for other attributes it is recommended to use the attr() method. to operate.

[Recommended learning:

jQuery video tutorial

,web front-end development]

The above is the detailed content of What are the commonly used methods to obtain attributes 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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!