Difference between the prop () method and the attr () method, and the attr Method

Source: Internet
Author: User

Difference between the prop () method and the attr () method, and the attr Method

Differences between the prop () method and the attr () method:
The specific usage of these two methods is not described here. You can refer to the prop () and attr () method manuals.
The two methods serve exactly the same purpose. In fact, the vast majority of them serve the same purpose. Only in some special circumstances can there be some differences, the following describes the differences between the two methods based on the instance.
Let's take a look at a code example:

<! DOCTYPE html> 

The above code is a very common function of Selecting All and all check boxes. In the code above, prop () is used to obtain or set the checked attribute value of the check box.
However, if the following code is used:

$('input[type=checkbox]').prop('checked',$(this).prop('checked'))

To:

$('input[type=checkbox]').prop('checked',$(this).attr('checked'))

In this way, we cannot implement the functions we want. The following is a brief analysis of the cause:
When you use the attr () method to obtain the checked attribute value of the check box, if the check box is selected, the obtained attribute value is checked. If not selected, the obtained attribute value is undefined, naturally, we cannot implement the functions we want. The prop () method can well make up for the shortcomings of the attr () method. This method returns true or false.
Two methods:
Some browsers only need to write disabled and checked, and some need to write disabled = "disabled", checked = "checked", for example, 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.
The selection principles of the two methods 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 ();
In fact, you don't have to be harsh. To avoid these troubles, just use prop.

The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 5814.

For more information, see: http://www.softwhy.com/jquery/

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.