Differences between attr () and prop () in jQuery when modifying the checked attribute _ jquery

Source: Internet
Author: User
Use statement $. attr (checked, true): Change the attribute of the check box to be selected. The first time you click "valid" in chrome, it won't work. IE8 is no problem when you select all the check boxes, A problem occurs. Use the statement $. attr ('checked', true): Change the attribute of the check box to be selected. The first time you click "valid" in chrome, it will not work. IE8 is correct.

Baidu has been searching for a long time because HTML attributes are divided into attribute and property. The latter is called a feature for the moment.

The checked attribute is divided into attribute-> checked, and property-> true, false.

For a checkbox, if checked = "checked" is not defined, the result of alert ($. attr ("checked") is undefined. If defined, the result is checked. Attribute does not change with the status of the checkbox.

If prop ($. attr ("checked") is used, the output values are false and true, respectively. Property changes as it changes.

Therefore, you must use prop () to modify the checked attribute (). Prop () is added after jQuery1.6.

Other charts with similar attributes on the Internet:

In versions earlier than IE9, if the property is not deleted before the DOM element is removed. setting the DOM element property (except for the simple type: number, string, and boolean) in the prop () method may cause memory leakage. To safely set the DOM object Value and avoid Memory leakage, you can use the. data () method. You have not encountered it yet. Remember it here first.

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.