Difference between Properties and Attributes in JavaScript
来源:互联网 发布:nosql与mysql的区别 编辑:程序博客网 时间:2024/05/16 08:06
IncTech开发者团队,承接App,微信,及其后台开发。更多精彩技术文章和技术研究,欢迎访问
我们的官网 http://www.inctech.cn
每一个dom节点,都有各自的attributes和properties。这两者很容易用混,尤其是在表单元素上面。
Property
每一个DOM节点,都是一个对象。像其他JS对象一样,DOM节点这类型HTMLElement对象,也可以添加一些方法或者属性。这些自定义添加的属性,就是property。它只能被JS所读取,并不会影响HTML的展示。(它能被JS的for-in方法遍历出来,但innerHTML里面不会显示)
Attribute
与Property不同,Attribute会DOM节点上显示出来,但不会在DOM对象中被for-in遍历出来。
例如上图的input标签,他的Attributies包括value,type,name,id。
想操作DOM元素的的attribute,得依靠下列的JS接口
需要注意的是
- 由于Attribute会显示在DOM上面,所以它的键名不区分大小写
- 它的值只可以是字符串
Attribute与Property之间的同步
自动同步
所有的DOM节点对象,都有一套标准的properties 。这些DOM对象的标准properties,会自动与其attributes同步。例如id\title\lang\dir\className
但有的properties与attributes的同步值,却并不是一样的
自动同步,但值并不完全相等
例如a标签的href属性
例如表单元素input的checked属性
只能从Attribute单向同步到Property
例如表单元素input的value属性
从Attribute同步到Property
Property却不能同步到Attribute
IE奇观
- 在IE6\7\8里面,propertites与attributies是自动同步的,并且attributies的值不限于字符串。
- 在IE6\7里面,propertites与attributies的键名不区分大小写。
总结
Properties就是JavaScript对象中的一个属性,而Attribute则是HTML元素中的一个属性。
- Difference between Properties and Attributes in JavaScript
- Difference among Field,Properties and Attributes in Programming laguages
- Difference between prototype and this in JavaScript
- Difference between enabled and userInteractionEnabled properties
- Difference between STA and MTA in thread
- Difference between Externalizable and Serializable in Java
- Difference between Vector and ArrayList in java?
- Difference between Comparator and Comparable in Java
- Difference between TreeMap and TreeSet in Java
- Difference between RegularEnumSet and JumboEnumSet in Java
- Difference between __str__ and __repr__ in Python
- Difference Between Thread and AsyncTask in Android
- Difference between Comparator and Comparable in java
- Difference between Stack and Heap in Java
- Difference between dialog and session in SIP?
- Difference between ArrayList and Vector In java
- Difference between Controller and Model in MVC
- Difference between datacontract and messagecontract in wcf
- C#泛型集合
- python3入门之赋值语句介绍
- 南邮 OJ 1054 回文回文!
- uva 10935 卡片游戏【队列】
- 【bzoj3620】 似乎在梦中见过的样子 KMP
- Difference between Properties and Attributes in JavaScript
- 设计模式之-----组合模式
- iOS contentOffset和contentInset的区别
- 关于Android的HAL的一些理解
- 【GDOI】模拟8.2总结
- 南邮 OJ 1055 叙拉古猜想
- Android分析主线程与子线程,以及子线程之间相互通信
- std::function与std::bind 函数指针
- 技术点V1.0