Difference between attr and prop in Jquery, jqueryattrprop

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.