最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript dom, how to handle "special properties" as versus attributes? - Stack Overflow

programmeradmin3浏览0评论

issue is whether to use property or attribute.

have not found this documented, so have run some tests (chromium 12):

property <=> attribute

accept, alt, formMethod, formTarget, id, name, placeholder, type, maxlength, size
form: method, name, target, action, enctype
  • can set either property or attribute
  • will reflect to property or attribute
  • exception 1: if form property will first look for element of that name (!)
  • exception 2: action property rewrites itself using value, passes set value to attribute
  • exception 3: enctype maintains its integrity, but passes set value to attribute

property <= attribute

value, autofocus, checked, disabled, formNoValidate, multiple, required
  • setting property has no effect on attribute
  • setting attribute also sets property

property => attribute

indeterminate
  • setting property also sets attribute
  • setting attribute has no effect on property

propetry >< attribute

defaultValue, validity, defaultChecked, readOnly
form: novalidate
  • setting property or attribute has no effect on the other


to me this seems pretty random behavior.

given a random attribute/value to apply to an element, here are the best rules I have e up with (modified as per Tim Down (thanks!) below):

  • if class, write with classList, read with className property

  • if a form, always read using attribute (and be a little careful)

  • if typeof element[propName] != "undefined", use property, ie, element[attr]=val

  • otherwise, use attribute, ie, element.setAttribute(attr,val)

is this even close to being right?

note: interestingly, if you have a form with an element named "novalidate", is it even possible to access the novalidate property of the form itself?

issue is whether to use property or attribute.

have not found this documented, so have run some tests (chromium 12):

property <=> attribute

accept, alt, formMethod, formTarget, id, name, placeholder, type, maxlength, size
form: method, name, target, action, enctype
  • can set either property or attribute
  • will reflect to property or attribute
  • exception 1: if form property will first look for element of that name (!)
  • exception 2: action property rewrites itself using value, passes set value to attribute
  • exception 3: enctype maintains its integrity, but passes set value to attribute

property <= attribute

value, autofocus, checked, disabled, formNoValidate, multiple, required
  • setting property has no effect on attribute
  • setting attribute also sets property

property => attribute

indeterminate
  • setting property also sets attribute
  • setting attribute has no effect on property

propetry >< attribute

defaultValue, validity, defaultChecked, readOnly
form: novalidate
  • setting property or attribute has no effect on the other


to me this seems pretty random behavior.

given a random attribute/value to apply to an element, here are the best rules I have e up with (modified as per Tim Down (thanks!) below):

  • if class, write with classList, read with className property

  • if a form, always read using attribute (and be a little careful)

  • if typeof element[propName] != "undefined", use property, ie, element[attr]=val

  • otherwise, use attribute, ie, element.setAttribute(attr,val)

is this even close to being right?

note: interestingly, if you have a form with an element named "novalidate", is it even possible to access the novalidate property of the form itself?

Share Improve this question edited Sep 22, 2014 at 11:45 Stephan Muller 27.6k18 gold badges86 silver badges127 bronze badges asked Aug 10, 2011 at 5:23 cc youngcc young 20.3k32 gold badges95 silver badges150 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 11

Except for rare special cases listed below, always use the property. Once the browser has parsed the intial HTML, attributes are no help to you unless you're serializing the DOM back to HTML for some reason.

Reasons to always favour properties:

  • dealing with properties is simpler (see Boolean properties such as checked: just use true and false and never worry whether you should be removing the attribute, or setting it to "" or "checked")
  • not every property maps to an attribute of the same name. For example, the checked property of a checkbox or radio button does not correspond to any attribute; the checked attribute corresponds to the defaultChecked property and does not change when the user interacts with the element (except in old IE; see next point). Likewise value and defaultValue.
  • setAttribute() and getAttribute() are broken in older versions of IE.

Special cases:

  • Attributes of <form> elements. Since each form input is mapped to a property of its parent <form> element corresponding to its name, it's safer to use setAttribute() and getAttribute() to obtain properties of the form such as action, name and method.
  • Custom attributes, e.g. <p myspecialinfo="cabbage">. These will not generally create equivalent properties on the DOM element object, so setAttribute() and getAttribute() should be used.

One final consideration is that there is not an exact correspondence between attribute and property names. For example, the property equivalent of the class attribute is className, and the property for the for attribute is htmlFor.

发布评论

评论列表(0)

  1. 暂无评论