DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密。很多新手朋友,也包括以前的我,经常会搞不清楚。

attribute翻译成中文术语为“特性”,property翻译成中文术语为“属性”,从中文的字面意思来看,确实是有点区别了,先来说说attribute。

attribute是一个特性节点,每个DOM元素都有一个对应的attributes属性来存放所有的attribute节点,attributes是一个类数组的容器,说得准确点就是NameNodeMap,总之就是一个类似数组但又和数组不太一样的容器。attributes的每个数字索引以名值对(name=”value”)的形式存放了一个attribute节点。

复制代码
代码如下:
<div class="box" id="box"
gameid="880">hello</div>

上面的div元素的HTML代码中有class、id还有自定义的gameid,这些特性都存放在attributes中,类似下面的形式:

复制代码
代码如下:
[ class="box", id="box", gameid="880"
]

可以这样来访问attribute节点:

复制代码
代码如下:
var elem = document.getElementById( 'box'
);
console.log( elem.attributes[0].name ); // class
console.log(
elem.attributes[0].value ); // box

但是IE6-7将很多东西都存放在attributes中,上面的访问方法和标准浏览器的返回结果又不同。通常要获取一个attribute节点直接用getAttribute方法:

复制代码
代码如下:
console.log( elem.getAttribute('gameid') ); //
880

要设置一个attribute节点使用setAttribute方法,要删除就用removeAttribute:

复制代码
代码如下:
elem.setAttribute('testAttr',
'testVal');
console.log( elem.removeAttribute('gameid') ); // undefined

attributes是会随着添加或删除attribute节点动态更新的。
property就是一个属性,如果把DOM元素看成是一个普通的Object对象,那么property就是一个以名值对(name=”value”)的形式存放在Object中的属性。要添加和删除property也简单多了,和普通的对象没啥分别:

复制代码
代码如下:
elem.gameid = 880; // 添加
console.log(
elem.gameid ) // 获取
delete elem.gameid // 删除

之所以attribute和property容易混倄在一起的原因是,很多attribute节点还有一个相对应的property属性,比如上面的div元素的id和class既是attribute,也有对应的property,不管使用哪种方法都可以访问和修改。

复制代码
代码如下:
console.log( elem.getAttribute('id') ); //
box
console.log( elem.id ); // box
elem.id = 'hello';
console.log(
elem.getAttribute('id') ); // hello

但是对于自定义的attribute节点,或者自定义property,两者就没有关系了。

复制代码
代码如下:
console.log( elem.getAttribute('gameid') );
// 880
console.log( elem.gameid ); // undefined
elem.areaid =
'900';
console.log( elem.getAttribute('areaid') ) // null

对于IE6-7来说,没有区分attribute和property:

复制代码
代码如下:
console.log( elem.getAttribute('gameid') );
// 880
console.log( elem.gameid ); // 880
elem.areaid =
'900';
console.log( elem.getAttribute('areaid') ) // 900

很多新手朋友估计都很容易掉进这个坑中。
DOM元素一些默认常见的attribute节点都有与之对应的property属性,比较特殊的是一些值为Boolean类型的property,如一些表单元素:

复制代码
代码如下:
<input type="radio" checked="checked"
id="raido">
var radio = document.getElementById( 'radio'
);
console.log( radio.getAttribute('checked') ); // checked
console.log(
radio.checked ); // true

对于这些特殊的attribute节点,只有存在该节点,对应的property的值就为true,如:

复制代码
代码如下:
<input type="radio" checked="anything"
id="raido">
var radio = document.getElementById( 'radio'
);
console.log( radio.getAttribute('checked') ); // anything
console.log(
radio.checked ); // true

最后为了更好的区分attribute和property,基本可以总结为attribute节点都是在HTML代码中可见的,而property只是一个普通的名值对属性。

复制代码
代码如下:
// gameid和id都是attribute节点
//
id同时又可以通过property来访问和修改
<div gameid="880" id="box">hello</div>

// areaid仅仅是property
elem.areaid = 900;

javascript中attribute和property的区别详解的更多相关文章

  1. 基于python中staticmethod和classmethod的区别(详解)

    例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object):   def foo(self,x):     print "executing foo ...

  2. DOS批处理中%cd%与%~dp0的区别详解

    转载:https://www.jb51.net/article/105325.htm DOS批处理中%cd%与%~dp0的区别详解     Windows下批处理中%cd%和%~dp0都能用来表示当前 ...

  3. JavaScript中的apply和call函数详解(转)

    每个JavaScript函数都会有很多附属的(attached)方法,包括toString().call()以及apply().听起来,你是否会感到奇怪,一个函数可能会有属于它自己的方法,但是记住,J ...

  4. JavaScript中的apply和call函数详解

    本文是翻译Function.apply and Function.call in JavaScript,希望对大家有所帮助 转自“http://www.jb51.net/article/52416.h ...

  5. Javascript中的局部变量、全局变量的详解与var、let的使用区别

    前言 Javascript中的变量定义方式有以下三种方式:1.直接定义变量,var与let均不写: a = 10; 2.使用var关键字定义变量 var a = 10; 3.使用let关键字定义变量 ...

  6. javascript中parentNode,childNodes,children的应用详解

    本篇文章是对javascript中parentNode,childNodes,children的应用进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助   "parentNode&qu ...

  7. Javascript中while和do-while循环用法详解

    while循环 while 语句与 if 语句相似,都有条件来控制语句(或语句块)的执行,其语言结构基本相同:while(conditions){    statements;} while 语句与 ...

  8. Javascript中的url编码与解码(详解)

    摘要 本文主要针对URI编解码的相关问题做了介绍,对url编码中哪些字符需要编码.为什么需要编码做了详细的说明,并对比分析了Javascript中和编解码相关的几对函数escape / unescap ...

  9. JavaScript中的this的指代对象详解

    在javascript里面,this是一个特殊的对象,它不像其他编程语言那样,是存储在实例中的值,直接指向此实例. 而是作为一个单独的指针,在不同的情况之下,指向不同的位置,这也是为什么我们会将它搞混 ...

随机推荐

  1. Git Bash 克隆project

    cd 到想要的路径,然后执行下面的命令

  2. Web打印的解决方案之证件套打

    由于以前未接触过套打,一直觉得套打是一个比较神秘和麻烦的事情,因为打印机的位置总是需要调整的,你总不能硬编码吧?但是如果位置可调,有需要直观一些来处理,那就比较麻烦了. 在前面介绍过<Web打印 ...

  3. 【推荐】安卓模板项目AndroidProject

    [推荐]安卓模板项目AndroidProject https://github.com/getActivity/AndroidProject 安卓架构 博客地址:但愿人长久,搬砖不再有 当我们日复一日 ...

  4. 将旧版本jQuery升级到新版本的jQuery

    需要将项目中的旧版本jQuery升级到新版本的jQuery,为解决兼容性问题得下载一个js兼容包.例子:升级的项目中jQuery1.x到jquery3.x,需要一个jquery-migrate-3.1 ...

  5. fgets注意事项

    这是yjy的习题库,中途我在使用fgest时颇费了一点心思,特此记录一下. #include <stdio.h> #include <string.h> #include &l ...

  6. JAVA中String空对象的字符串拼接

    今天使用JSONObject中get一个不存在的对线,最后拼接成sql语句插入数据库时,最后数据库中的值为字符串'null',而不是空对象. 追踪许久才发现自己的java白学了. java strin ...

  7. SAGAN:Self-Attention Generative Adversarial Networks - 1 - 论文学习

    Abstract 在这篇论文中,我们提出了自注意生成对抗网络(SAGAN),它是用于图像生成任务的允许注意力驱动的.长距离依赖的建模.传统的卷积GANs只根据低分辨率图上的空间局部点生成高分辨率细节. ...

  8. 【GMT43智能液晶模块】例程十六:LAN_TCPS实验——以太网数据传输

    源代码下载链接: 链接:https://pan.baidu.com/s/1e5Qp-xASjlA0pje3S7TdIg提取码:9v37 复制这段内容后打开百度网盘手机App,操作更方便哦 GMT43购 ...

  9. pauseable 库

    pauseable.js https://www.npmjs.com/package/pauseable Pauseable allows you to pause event emitters, t ...

  10. 【LeetCode算法-53】Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...