After the prop method is introduced in higher versions of jquery, when should prop be used? When to use attr? What's the difference between the two of them? These problems arise.
There are many answers online about the difference between the two. Let me talk about my thoughts here. My thoughts are very simple:
• For the inherent attributes of the HTML element itself, use the prop method when processing.
• For our own custom DOM attributes of HTML elements, use the attr method when processing them.
The above description may be a bit vague, just give a few examples.
In this example, the DOM attributes of the element include "href, target and class". These attributes are the attributes of the element itself. These attributes are also included in the W3C standard. In other words, the attributes that can be intelligently prompted in the IDE are called inherent attributes. When dealing with these properties, it is recommended to use the prop method.
Another example:
For elements such as checkbox, radio and select, the checked attributes correspond to "checked" and "selected". These are also inherent attributes, so you need to use the prop method to operate to obtain the correct results.
If the attr method is used above, it will appear:
Full text completed.
The above is the entire content of this article, I hope you all like it.