美文网首页前端开发那些事儿
attribute和property的区别

attribute和property的区别

作者: vivianXIa | 来源:发表于2021-03-29 15:52 被阅读0次

看到别的文章 要么格式乱 要么没读懂这篇文章个人感觉恰到好处

第一个问题: 什么是 attribute & 什么是 property ?

attribute 是我们在 html 代码中经常看到的键值对, 例如:

<input id="the-input" type="text" value="Name:" />

上面代码中的 input 节点有三个 attribute:

  • id : the-input
  • type : text
  • value : Name:

property 是 attribute 对应的 DOM 节点的 对象属性 (Object field), 例如:

HTMLInputElement.id === 'the-input'
HTMLInputElement.type === 'text'
HTMLInputElement.value === 'Name:'

第二个问题:

从上面的例子来看, 似乎 attribute 和 property 是相同的, 那么他们有什么区别呢?

让我们来看另一段代码:

<input id="the-input" type="typo" value="Name:" /> // 在页面加载后,
我们在这个input中输入 "Jack"

注意这段代码中的 type 属性, 我们给的值是 typo, 这并不属于 input 支持的 type 种类.

让我们来看看上面这个 input 节点的 attribute 和 property:

// attribute still remains the original value
input.getAttribute('id') // the-input
input.getAttribute('type') // typo
input.getAttribute('value') // Name:

// property is a different story
input.id // the-input
input.type //  text
input.value // Jack

可以看到, 在 attribute 中, 值仍然是 html 代码中的值. 而在 property 中, type 被自动修正为了 text, 而 value 随着用户改变 input 的输入, 也变更为了 Jack

这就是 attribute 和 Property 间的区别:

attribute 会始终保持 html 代码中的初始值, 而 Property 是有可能变化的.

其实, 我们从这两个单词的名称也能看出些端倪:

attribute 从语义上, 更倾向于不可变更的

property 从语义上更倾向于在其生命周期中是可变的

Attribute or Property 可以自定义吗?

先说结论: attribute 可以 property 不行

我们可以尝试在 html 中自定义 attribute:

<input value="customInput" customeAttr="custome attribute value" />

然后我们尝试获取自定义的属性:

input.getAttribute('customAttr') // custome attribute value
input.customAttr // undefined

可以看到, 我们能够成功的获取自定义的 attribute, 但是无法获取 property.

其实不难理解, DOM 节点在初始化的时候会将html 规范中定义的 attribute 赋值到 property 上, 而自定义的 attribute 并不属于这个氛围内, 自然生成的 DOM 节点就没有这个 property.

一些补充

需要注意, 有一些特殊的 attribute, 它们对应的 Property 名称会发生改变, 比如:

  • for (attr) => htmlFor (prop)
  • class (attr) => className (prop)

(如果我们追到 DOM 的源码中, 应该是能列出一份清单的: 哪些 attribute 的名称会对应到哪些 Property, 感兴趣不妨试试)

关于 attribute 和 property 两者之间的差别, stackoverflow 上有一些很有意思的讨论:

https://stackoverflow.com/a/6377829/5033286

其中有些人认为 attribute 的值表示的是 defaultValue, 而 DOM 节点的 Property 则是另一回事. 比如: check (attr) 对应的是 defaultChecked (prop), value(attr) 对应的应该是 defaultValue (prop)

转载于https://zhuanlan.zhihu.com/p/49536969

个人总结

  • 对于html的标准属性来说,attribute和property是同步的,是会自动更新的
  • 但是对于自定义的属性来说,他们是不同步的,property会同步不到
  • attribute是dom元素在文档中作为html标签拥有的属性
  • property就是dom元素在js中作为对象拥有的属性

相关文章

  • JavaScript 常见面试题分析(三)

    01 property 和 attribute 的区别 property 修改对象属性,不会体现到 HTML 结构...

  • Attribute和Property的区别

    1. Attribute和Property的概念 attribute : 特性,XML 元素中的概念,用于描述 X...

  • property和attribute的区别

    property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,at...

  • attribute和property的区别

    看到别的文章 要么格式乱 要么没读懂这篇文章个人感觉恰到好处 第一个问题: 什么是 attribute & 什么是...

  • property和attribute

    property和attribute 两种有何区别? property指的是 dom 本身内置的属性,比如tagN...

  • property&attribute

    2017年4月6日DOM 中 Property 和 Attribute 的区别转载:博客园property 和 a...

  • attr与prop

    attr和prop分别是单词attribute和property的缩写(property:属性,attribute...

  • web小结

    DOM操作部分 DOM的数据结构是一种树 attribute和property的区别 节点的property 节点...

  • HTML property 和 attribute 的区别

    前端编程里,property 和 attribute 是一对极容易混淆的术语。 Angular 的属性绑定语法: ...

  • 2.Attribute 和 property区别

    DOM元素的特性(Attribute)和属性(Property)1.property 只是一个js对象的属性的修改...

网友评论

    本文标题:attribute和property的区别

    本文链接:https://www.haomeiwen.com/subject/mlubhltx.html