Home > Web Front-end > JS Tutorial > What is the difference between jquery prop() and attr()

What is the difference between jquery prop() and attr()

coldplay.xixi
Release: 2020-11-25 14:23:05
Original
6091 people have browsed it

The difference between jquery prop() and attr(): 1. The [attr()] method sets or returns the attributes and values ​​of the selected element; 2. The [prop()] method sets or returns the attributes and values ​​of the selected element. Properties and values.

What is the difference between jquery prop() and attr()

  • This method is suitable for all brands of computers

jquery prop() Differences from attr():

Recent projects have returned to using jquery, and page rendering is all done using jquery, so I encountered many problems that I have never seen before, such as this operation [ Radio] I occasionally encounter problems with the "checked" attribute of the control.

$("...").attr("checked",false); does not work, and the Internet I checked and found that using prop() perfectly solved the problem, so I will record it here.

Official definition: attr():

attr() method sets or returns the attributes 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.

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

prop():

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.

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

  • $('').attr()Returns the html object

  • $(' ').prop()returns the DOM object

prop()

method operates controls such as radio (checkbox) to select time, the selected value of its control will also change accordingly. That is, you can control both its selection and its deselection;

is similar to $("...").attr("checked"); returns true or false

If there is a corresponding attribute, the attribute is returned. If not, an empty string is returned.

attr():

Method operation## When controls such as #adio (checkbox) are selected, the selected value of the control will not change accordingly. That is, you can only control its selection, but not its deselection;

is similar to

$("...").attr("checked");The returned value is 'checked' or undefined

If there is a corresponding attribute, the attribute is returned. If not, undefined is returned.

Usage scenarios of attr and prop:

1 .Add the attribute name and the attribute will take effect. You should use prop();

2. If there are two attributes, true and false, use prop(); (such as 'checked', 'selected', 'disabled', etc. )

3. For others, use attr();

Official recommendation:

What is the difference between jquery prop() and attr()

Related free learning recommendations:

JavaScript(Video)

The above is the detailed content of What is the difference between jquery prop() and attr(). 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