Differences between attr and prop in jquery-check box selection status, jqueryattr
In recent projects, you need to use jquery to determine whether the checkbox in the input is selected. It is found that the status of the check box changed cannot be obtained using attr (). Finally, it is found that prop () is added after jQuery 1.6 () method:
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.
The above translation is probably: prop () processes the attributes that come with the Node object. It contains many native properties, while attr () comes from the attributes of the object, it may be because we add attribute nodes to element nodes. Of course, the previous translation only represents my opinion. If you have any mistakes, you can submit them ~~
In simple terms:
- For the inherent attributes of HTML elements, the prop method is used for processing.
- For DOM attributes customized by HTML elements, the attr method is used for processing.
The above description may be a bit vague. I will refer to some of my Blog content and add my tests. Let's take a few examples.
Example 1: inherent and custom attributes of an element
<A href = "http://www.baidu.com" target = "_ self" class = "btn"> Baidu </a>
In this example, the DOM attributes of <a> elements include "href, target, and class". These attributes are the attributes that <a> elements themselves carry, these attributes are included in W3C standards, or attributes that can be intelligently displayed in IDE. These attributes are inherent attributes. When processing these attributes, we recommend that you use the prop method.
<A href = "#" id = "link1" action = "delete"> delete </a>
In this example, the DOM attributes of <a> elements include "href, id, and action". Obviously, the first two attributes are inherent, the next "action" attribute is customized by ourselves. <a> the element itself does not have this attribute. This is the custom DOM attribute. We recommend that you use the attr method when processing these attributes. When the prop method is used to set values and attribute values, the undefined value is returned.
Example 2: differences between prop () and attr () in Form applications
Let's take a look at the code using the attr () method:
<! DOCTYPE html>
The test result of the above program is:
From the dynamic graph, we can see that the selected state cannot be obtained using attr (), and only the initial value of the form is returned.
Let's take a look at the code using the prop () method:
<! DOCTYPE html>
The test result of the above program is:
As can be seen from the dynamic graph, prop () can be used to obtain the user's selected and canceled operation status in real time.
The difference I have summarized is:The value of the property record is updated in real time according to user operations, while the attribute record is an initial value. Only represent my opinion ~ \ (Too many rows )/~ La la
After writing for so long, what I want to talk about is that it is best to write with the native js Code. Although writing with the jquery plug-in is fast, jquery will delete and add some attributes each time it is updated, if you do not learn it in time, it will be trapped by some small bugs ~~~