Differences between the jqueryprop method and the attr Method

Source: Internet
Author: User
Day! Finally, I knew what was wrong last time? To know the differences between the jqueryprop method and the attr method, you must first understand the concepts of property and attribute: 1. We know that for the checkbox DOM object, it must have

Day! Finally, I knew what was wrong last time? To know the difference between the jquery prop method and the attr method, you must first understandConcepts of property and attribute:
1. we know that for the checkbox DOM object, it must have a selection status, and there are only two options, either selected or not selected, therefore, no matter when we use the prop method to obtain its checked value, we can get a definite value, true or false. For the HTML representation of the checkbox, it can be simply expressedHere, you do not need to assign values to the checked attribute. In this case, when you use attr to obtain the checked value, the value will be undefined.

2. For some HTML tag attributes, different attribute names will be matched in the DOM object, such

  • Its attribute name is class, but its property name is className.

    3. If you use prop to assign values, the current State is assigned values. For some attributes, attr assigns an initial value. In this way, prop will always change the status of the checkbox dynamically. For Some browsers, attr can only change the initial status of the checked before the checkbox is displayed on the page, then, the checked status cannot be changed. In addition, the prop can always get the current status of the checkbox, regardless of the current status of the checkbox, attr can only get the initial status.

    4. the built-in DOM property cannot be removed, but attribute can.
    <以上四点参考资料:http: www.cnblogs.com studynote p 3475005.html>
    Let's talk about it again. Difference between the prop method and the attr method:
    Currently, I only encounter differences in checkbox, but IE is not compatible. The official differences are as follows:
    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 ();

  • 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.