Difference between attr and prop in Jquery, jqueryattrprop
Result of the prop () function:
1. If a property exists, the specified property value is returned.
2. If no corresponding attribute exists, the returned value is a null string.
Result of the attr () function:
1. If a property exists, the specified property value is returned.
2. If no corresponding attribute exists, the returned value is undefined.
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.
Properties with both true and false attributes, such as checked, selected, or disabled using prop ()
Differences between attr and prop in jquery:
• Use the prop method for processing inherent attributes of HTML elements.
• The attr method is used for processing custom DOM attributes of HTML elements.
Chestnut 1:
<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 contained in the <a> element, which are also included in the W3C standard, or intelligently prompted attributes in the IDE, these 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.