getAttribute获取的是你是实际设置的值。而点号返回的是布尔值。 浏览器兼容性上的差别 1.在IE<9的浏览器中,可以用点号和getAttribute在相互之间访问自定义属性。 2.IE<8(包括IE8种的IE7兼容模式),property和attribute相同。因为attribute对大小写不敏感,在这种情况下,用getAttribute访问特性的时候,浏览器会选择第一次出现的值。 复制代码 代码如下: document.body.abba = 1 // assign property (now can read it by getAttribute) document.body.ABBA = 5 // assign property with another case // must get a property named "ABba" in case-insensitive way. alert( document.body.getAttribute("ABba") ) // 1
优先选择property 在实际应用中,98%的 DOM 操作都是使用 properties。 只有两种情形需要使用attributes 1.自定义 HTML attributes,因为它并不同步到DOM property。 2.访问内置的 HTML attributes,这些 attribute 不能从 property 同步过来。例如 INPUT标签的value值。