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/