Differences between the prop () method and the ATTR () method in jquery

Source: Internet
Author: User

Jquery1.6 adds a method prop (), which has never been used. The official explanation is only one sentence: Get the attribute value of the first element in the matched element set.

The official examples are similar to ATTR () And I don't know what the difference is. Since the new prop () method is available, it cannot be used. When should I use ATTR (), when should I use prop?

We all know that some browsers only need to write disabled and checked, while some need to write Disabled = "disabled", checked = "checked", such as ATTR ("checked ") when obtaining the checked attribute of the checkbox, you can obtain the value when it is selected. The value is "checked", but the undefined value is not selected.

JQ provides a new method "prop" to obtain these attributes to solve this problem. Previously, when we used ATTR to obtain the checked attribute, we returned "checked" and "", if you use the prop method to obtain attributes, both true and false are returned.

So when will ATTR () and prop () be used ()?
1. Add a property name. This property will take effect. Use prop ();
2. There are true and false attributes using prop ();
3. For others, use ATTR ();
Pay attention to this when upgrading jquery in the project!

We recommend that you use ATTR () and prop () as follows:

Attribute/Property .attr() .prop()
Location (I. e. Window. Location)
Width (if needed over.width())  

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.