The difference between prop () method and attr () method in jquery-basic knowledge

Source: Internet
Author: User

jquery1.6 a new method prop (), has never used it, the official explanation is only one sentence: Gets the property value of the first element in the matching set of elements.

We all know that some browsers as long as the disabled,checked can be written, and some to write disabled = "disabled", checked= "checked", such as with attr ("checked") When the checkbox's Checked property is fetched, the value can be taken, the value is "checked", but the fetch value is not selected undefined.

JQ provides a new method "prop" to get these properties, which is to solve the problem, we used attr to get checked properties when we returned "checked" and "", and now we use the prop method to get the property then the uniform returns True and false.

So, when to use attr (), when to use Prop ()?
1. Add attribute name This property will take effect should use Prop ();
2. There are true,false two attributes using prop ();
3. Others use attr ();
You should pay attention to this when you upgrade jquery in your project!

The following is the use of the official recommendation attr (), prop ():

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() )
Related Article

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.