Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第159天 在js中attribute和property的区别是什么? #1235

Open
haizhilin2013 opened this issue Sep 21, 2019 · 10 comments
Open

[js] 第159天 在js中attribute和property的区别是什么? #1235

haizhilin2013 opened this issue Sep 21, 2019 · 10 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第159天 在js中attribute和property的区别是什么?

@haizhilin2013 haizhilin2013 added the js JavaScript label Sep 21, 2019
@vkboo
Copy link

vkboo commented Sep 22, 2019

  • attribute

    • 元素在HTML中的键值对
    • attribute 会始终保持 html 代码中的初始值(除了href)
  • property

    • attribute在对应的JS DOM节点上的对象属性
    • Property是有可能变化的(跟随用户操作).
  • 其他补充

@justjavac
Copy link

@vkboo 链接多了一半括号 https://zhuanlan.zhihu.com/p/70671215

@forever-z-133
Copy link

forever-z-133 commented Sep 23, 2019

@justjavac 的最后一项,
更新 value 时,属性和特性的更新是什么样的呀

@wqcstrong
Copy link

@vkboo 链接多了一半括号 https://zhuanlan.zhihu.com/p/70671215

@justjavac 在理解的基础上,用一句话概括:浏览器解析之前全都是特性,解析之后分情况:标准特性的值将被解析并挂载到 DOM属性上,非标准特性只可用DOM的 getAttribute 等方法取值。

这样说应该问题不大吧?

@justjavac
Copy link

@foreverZ133 非标准的不做任何映射,只能通过 getAttribute 等函数获取到值,值是字符串类型。

@forever-z-133
Copy link

forever-z-133 commented Sep 23, 2019

@foreverZ133 非标准的不做任何映射,只能通过 getAttribute 等函数获取到值,值是字符串类型。

噢,一个会改变 html 一个不会,突然冒出来个特性搞蒙了,Thanks

@zhaofeipeter
Copy link

zhaofeipeter commented Jul 30, 2020

1、property和attributies都是properties的子集,而每个attribute是attributies的子集;
2、attribute可以理解为特性,可以自定义,直接在html标签上添加的和使用setAttribute添加的情况一致,即html标签添加的都是attribute属性, property则是使用xx.属性进行更改,通常来讲,更改互相影响(value除外)
3、当添加新的非默认属性时,是不互通的;
4、一些特殊属性,则需要特殊对待。

property能够从attribute中得到同步;
attribute不会同步property上的值;
attribute和property之间的数据绑定是单向的,attribute->property;
更改property和attribute上的任意值,都会将更新反映到HTML页面中;

@bozaigao
Copy link

bozaigao commented Dec 2, 2020

property是DOM中的属性,DOM是JavaScript里的对象;
attribute是HTML标签上的特性,它的值只能够是字符串;

@xiaoqiangz
Copy link

attribute是特性,一般是用于自定义特性,例:data-xx=“xxx” 可以使用getAttribute('data-xx')获取,且只能是字符串。
property是属性,是dom元素中的属性,例如dom.innerHTML会改变dom本身。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

9 participants