Home > Article > Web Front-end > What is the difference between jquery prop() and attr()
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.
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;
$("...").attr("checked");The returned value is 'checked' or undefined
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: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!