When is it preferable to use `attr()` instead of `.prop()`?

This question already has an answer here:

  • .prop() vs .attr() 17 answers

  • Applicable before jQuery 1.9

    Below is a list of some attributes and properties and which method should normally be used when getting or setting them. This is the preferred usage, but the .attr() method will work in all cases.

    +------------------------------------+------------+-----------+
    | Attribute/Property                 |  .attr()   |  .prop()  |
    +------------------------------------+------------+-----------+
    | accesskey                          |    ✓       |           |
    | align                              |    ✓       |           |
    | async                              |            |    ✓      |
    | autofocus                          |            |    ✓      |
    | checked                            |            |    ✓      |
    | class                              |    ✓       |           |
    | contenteditable                    |    ✓       |           |
    | disabled                           |            |    ✓      |
    | draggable                          |    ✓       |           |
    | href                               |    ✓       |           |
    | id                                 |    ✓       |           |
    | label                              |    ✓       |           |
    | location (i.e., window.location)   |            |    ✓      |
    | multiple                           |            |    ✓      |
    | readOnly                           |            |    ✓      |
    | rel                                |    ✓       |           |
    | selected                           |            |    ✓      |
    | src                                |    ✓       |           |
    | tabindex                           |    ✓       |           |
    | title                              |    ✓       |           |
    | type                               |    ✓       |           |
    | width (if needed over .width())    |    ✓       |           |
    +------------------------------------+------------+-----------+
    

    Neither .attr() nor .prop() should be used for getting/setting value. Use the .val() method instead (although using .attr(“value”, “somevalue”) will work.

    Summary: The .prop() method should be used for boolean attributes/properties and for properties which do not exist in html (such as window.location ). All other attributes (ones you can see in the html) can and should continue to be manipulated with the .attr() method.

    Reference


    The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

    For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.

    Concerning boolean attributes, consider a DOM element defined by the HTML markup , and assume it is in a JavaScript variable named elem:

    elem.checked true (Boolean) Will change with checkbox state $(elem).prop("checked") true (Boolean) Will change with checkbox state

    elem.getAttribute("checked") "checked" (String) Initial state of the checkbox; does not change $(elem).attr("checked") (1.6) "checked" (String) Initial state of the checkbox; does not change

    $(elem).attr("checked") (1.6.1+) "checked" (String) Will change with checkbox state $(elem).attr("checked") (pre-1.6) true (Boolean) Changed with checkbox state

    SOURCE

    链接地址: http://www.djcxy.com/p/27540.html

    上一篇: jquery 1.6.2问题使用.prop()进行检查和禁用

    下一篇: 什么时候最好使用`attr()`而不是`.prop()`?