HTML attribute vs. DOM property

要想理解 Angular 绑定如何工作,重点是搞清 HTML attribute 和 DOM property 之间的区别。

attribute 是由 HTML 定义的。property 是由 DOM (Document Object Model) 定义的。

  • 少量 HTML attribute 和 property 之间有着 1:1 的映射,如id

  • 有些 HTML attribute 没有对应的 property,如colspan

  • 有些 DOM property 没有对应的 attribute,如textContent

  • 大量 HTML attribute看起来映射到了property…… 但却不像我们想的那样!

    Many HTML attributes appear to map to properties ... but not in the way you might think!

最后一类尤其让人困惑…… 除非我们能理解这个普遍原则:

attribute 初始化 DOM property,然后它们的任务就完成了。property 的值可以改变;attribute 的值不能改变。

例如,当浏览器渲染<input type="text" value="Bob">时,它将创建相应 DOM 节点, 其value property 被初始化为 “Bob”。

当用户在输入框中输入 “Sally” 时,DOM 元素的value property 变成了 “Sally”。 但是这个 HTML value attribute 保持不变。如果我们读取 input 元素的 attribute,就会发现确实没变: input.getAttribute('value') // 返回 "Bob"

HTML attribute value指定了初始值;DOM value property 是当前值。

disabled attribute 是另一个古怪的例子。按钮的disabled property 是false,因为默认情况下按钮是可用的。 当我们添加disabledattribute 时,只要它出现了按钮的disabled property 就初始化为true,于是按钮就被禁用了。

添加或删除disabled attribute会禁用或启用这个按钮。但 attribute 的值无关紧要,这就是我们为什么没法通过 <button disabled="false">仍被禁用</button>这种写法来启用按钮。

设置按钮的disabled property(如,通过 Angular 绑定)可以禁用或启用这个按钮。 这就是 property 的价值。

就算名字相同,HTML attribute 和 DOM property 也不是同一样东西。

HTML attribute 与 DOM property 的对比的更多相关文章

  1. HTML的attribute和DOM的property剖析(转)

    原文:https://www.jianshu.com/p/efc704d713c7 HTML attribute 与 DOM property 的对比 该文摘自angular的官方文档,老外对概念的解 ...

  2. 彻底区分html的attribute与dom的property

    当初在学html时始终没有弄清楚的关于attribute与property的区别,竟然在看angular文档时弄明白了. angular官方文档的数据绑定一节提到html attribute与dom ...

  3. DOM元素的Attribute(特性)和Property(属性) 【转载】

    1.介绍: 上篇js便签笔记http://www.cnblogs.com/wangfupeng1988/p/3626300.html最后提到了dom元素的Attribute和Property,本文简单 ...

  4. Attribute和自定义Property

    property(属性) attribute(特性) property和attribute的同步 id href value class/className 旧IE的趣事 attribute作为DOM ...

  5. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

  6. 前端基本知识(一):W3C标准&&冒泡事件,捕获事件,W3C DOM对象模型,对比分析

    W3C标准是万维网联盟, 其他的可以参考万维网版本的更新内容 一.W3C标准 二.W3C DOM事件 三.冒泡事件 四.捕获事件 一.W3C标准 其实网页是由三分部组成:1.结构(structure) ...

  7. 前端中的 Attribute & Property

    为了在翻译上显示出区别,Attribute一般被翻译为特性,Property被译为属性. 在使用上面,Angular已经表明态度 Template binding works with propert ...

  8. angular2的模板语法

    Angular 应用管理着用户之所见和所为,并通过 Component 类的实例(组件)和面向用户的模板来与用户交互. 从使用模型-视图-控制器 (MVC) 或模型-视图-视图模型 (MVVM) 的经 ...

  9. boolean attribute(布尔值属性) attribute vs property

    boolean attribute(布尔值属性) boolean attribute     HTML - Why boolean attributes do not have boolean val ...

随机推荐

  1. code1173 最优贸易

    先正向从1点出发SPFA,获得min[i],就是到达i点能最低购买到的价格,(起始点到i的路上经过的最小值) 然后反向(将图反向),从n点开始SPFA,获得max[i],就是从i点到终点能够卖出的最大 ...

  2. HTTP 499状态码 nginx下499错误详解-乾颐堂

    日志记录中HTTP状态码出现499错误有多种情况,我遇到的一种情况是nginx反代到一个永远打不开的后端,就这样了,日志状态记录是499.发送字节数是0. 老是有用户反映网站系统时好时坏,因为线上的产 ...

  3. Spring Boot☞ 使用Spring-data-jpa简化数据访问层

    效果图: 代码区: package com.wls.integrateplugs.jpa.primary.model; /** * Created by wls on 2017/8/24. */ im ...

  4. java.lang.ClassCastException: com.liuyang.annocation.UserAction cannot be cast to com.liuyang.annocation2.UserAction at com.liuyang.annocation2.App.test

    java.lang.ClassCastException: com.liuyang.annocation.UserAction cannot be cast to com.liuyang.annoca ...

  5. CodeForces 289A Polo the Penguin and Segments (水题)

    题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...

  6. Linux下MySQL表名不区分大小写的设置方法

    MySQL表名不区分大小写的设置方法 在用centox安装mysql后,把项目的数据库移植了过去,发现一些表的数据查不到,排查了一下问题,最后发现是表名的大小写不一致造成的. mysql在window ...

  7. 【Linux】GCC编译器

    [简介] GCC是Linux下的编译工具集,是GNU Compiler Collection的缩写,包含gcc g++ 等编译器.GCC工具集不仅能编译C/C++语言,其他例如Object-c.Pas ...

  8. 7、Semantic-UI之图标与按钮组

    7.1 图标按钮   Semantic-UI中可以定义一组图标样式,并且可以在按钮中使用图标. 示例:定义一个图标按钮 <button class="ui black button&q ...

  9. Android ActionBar使用方法

    对于这ActionBar我想很多人都想了解一下到底是怎么一个使用方法,以及它都存在哪些可操作的和使用的地方.如下图所示:<ignore_js_op> 这便是ActionBar的基本内容.获 ...

  10. selenium三大浏览器driver下载地址

    Chrome 点击下载chrome的webdriver: http://chromedriver.storage.googleapis.com/index.html 不同的Chrome的版本对应的ch ...