Differences between the prop () method and the attr () method in jquery _ basic knowledge-js tutorial

Source: Internet
Author: User
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 ()? A method prop () has been added in jquery1.6 and has never been used. The official explanation is only one sentence: Get the attribute value of the first element in the matched element set.

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 ") you can obtain the value when you select the checked attribute of the checkbox. 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. In the past, 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()
Accesskey
Align
Async
Autofocus
Checked
Class
Contenteditable
Draggable
Href
Id
Label
Location (I. e. window. location)
Multiple
ReadOnly
Rel
Selected
Src
Tabindex
Title
Type
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.