javascript dom,如何处理“特殊属性” as 与属性?

发布于 2024-11-28 16:24:18 字数 1481 浏览 2 评论 0原文

问题是是否使用属性或属性。

尚未找到此记录,因此运行了一些测试(chromium 12):

property <=>; attribute

accept, alt, formMethod, formTarget, id, name, placeholder, type, maxlength, size
form: method, name, target, action, enctype
  • 可以设置 property 或 attribute,
  • 将反映到 property 或 attribute
  • 异常 1:如果表单属性将首先查找该名称的元素 (!)
  • 异常 2:action 属性使用值重写自身,将设置值传递给属性
  • 异常3:enctype保持其完整性,但将设置值传递给属性

property<=attribute

value, autofocus, checked, disabled, formNoValidate, multiple, required
  • 设置property对属性没有影响设置
  • attribute也设置property

property==>属性

indeterminate
  • 设置属性也设置属性
  • 设置属性对属性没有影响

财产>>< 给定应用于元素的随机属性/值,设置属性

defaultValue, validity, defaultChecked, readOnly
form: novalidate
  • 或属性对另一个没有影响


to me this seems pretty random behavior.

,这是我想出的最佳规则(根据下面的 Tim Down(谢谢!)进行修改):

  • 如果是类,用classList写入,用className属性读取

  • 如果是表单,则始终使用属性读取(并且要小心一点)

  • if typeof element[propName] != "undefined",使用属性,即 element[attr]=val

  • 否则,使用属性,即 element.setAttribute(attr,val)

这是否接近正确?

注意:有趣的是,如果您有一个包含名为“novalidate”元素的表单,是否可以访问表单本身的 novalidate 属性?

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 come 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-12-05 16:24:18

除下面列出的罕见特殊情况外,请始终使用该属性。一旦浏览器解析了初始 HTML,属性就对您没有任何帮助,除非您出于某种原因将 DOM 序列化回 HTML。

总是青睐属性的原因:

  • 处理属性更简单(请参阅布尔属性,例如 checked:只需使用 truefalse ,而不必担心您是否应该删除该属性,或将其设置为 """checked")
  • 并非每个属性都映射到同名的属性。例如,复选框或单选按钮的 checked 属性不对应于任何属性; checked 属性对应于 defaultChecked 属性,并且当用户与元素交互时不会更改(旧版 IE 中除外;请参阅下一点)。 valuedefaultValue 也是如此。
  • setAttribute()getAttribute()损坏在旧版本的 IE 中

特殊情况:


  • 元素的属性。由于每个表单输入都映射到与其 name 对应的父 元素的属性,因此使用 setAttribute() 更安全和getAttribute()来获取表单的属性,例如actionnamemethod
  • 自定义属性,例如

    。这些通常不会在 DOM 元素对象上创建等效属性,因此应使用 setAttribute()getAttribute()

最后一个考虑因素是属性和属性名称之间不存在精确的对应关系。例如,class 属性的等效属性是 classNamefor 属性的属性是 htmlFor

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文