attributes vs properties

This question already has an answer here:

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

  • Attributes are defined by HTML. Properties are defined by DOM.

    Some HTML attributes have 1:1 mapping onto properties. id is one example of such.

    Some do not (eg the value attribute specifies the initial value of an input, but the value property specifies the current value).


    Yes, attr is meant for html attributes as they are strictly defined. prop is for properties.

    So for instance, say you have a node elem with class "something" (raw element not jQuery object). elem.className is the property, but is where the attribute resides. Changing the class attribute also changes the property automatically and vise versa.

    Currently, attr is jumbled and confusing because it has tried to the job of both functions and there are many bugs because of that. The introduction of jQuery.fn.prop will solve several blockers, separate code as it should have been separated from the beginning, and give developers faster functions to do what they expect them to do.

    Let me make up a percentage for a sec and say that from my experience in the support IRC and reading other's code, 95% of the use cases for attr will not have to switch to prop.

    See More

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

    上一篇: 为什么“〜undefined”是

    下一篇: 属性与属性