attribute和property的区别

property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴。

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

基于JavaScript分析property 和 attribute

html中有这样一段代码:

<input id="in_1" value="1" sth="whatever">

简单的在html页面上创建一个input输入栏(注意在这个标签中添加了一个DOM中不存在的属性“sth”),此时在JS执行如下语句

var in1 = document.getElementById('in_1');

执行语句

var input1 =document.getElementsByTagName('input')[0];
console.log(input1);  

从console的打印结果,可以看到in1含有一个名为“attributes”的属性,它的类型是NamedNodeMap,同时还有“id”和“value”两个基本的属性,但没有“sth”这个自定义的属性。

attributes: NamedNodeMap
value: "1"
id: "in_1"

有些console可能不会打印in1上的属性,那么可以执行以下命令打印要观察的属性:

console.log(in1.id);        // 'in_1'
console.log(in1.value);     // 1
console.log(in1.sth);       // undefined

可以发现,标签中的三个属性,只有“id”和“value”会在in1上创建,而“sth”不会被创建。这是由于,每一个DOM对象都会有它默认的基本属性,而在创建的时候,它只会创建这些基本属性,我们在TAG标签中自定义的属性是不会直接放到DOM中的。

我们做一个额外的测试,创建另一个input标签,并执行类似的操作:

html:

<input id="in_2">

JS:

var input2 = document.getElementsByTagName('input')[1];
console.log(input2);

从打印信息中可以看到:

id: "in_2"
value: null

尽管我们没有在TAG中定义“value”,但由于它是DOM默认的基本属性,在DOM初始化的时候它照样会被创建。由此我们可以得出结论:

  • DOM有其默认的基本属性,而这些属性就是所谓的“property”,无论如何,它们都会在初始化的时候再DOM对象上创建。
  • 如果在TAG对这些属性进行赋值,那么这些值就会作为初始值赋给DOM的同名property。

现在回到第一个input(“#in_1”),我们就会问,“sth”去哪里了?别急,我们把attributes这个属性打印出来看看

console.log(input1);

上面有几个属性:

0: id
1: value
2: sth
length: 3
__proto__: NamedNodeMap

原来“sth”被放到了attributes这个对象里面,这个对象按顺序记录了我们在TAG中定义的属性和属性的数量。此时,如果再将第二个input标签的attributes打印出来,就会发现只有一个“id”属性,“length”为1。

从这里就可以看出,attributes是属于property的一个子集,它保存了HTML标签上定义属性。如果再进一步探索attitudes中的每一个属性,会发现它们并不是简单的对象,它是一个Attr类型的对象,拥有NodeType、NodeName等属性。关于这一点,稍后再研究。注意,打印attribute属性不会直接得到对象的值,而是获取一个包含属性名和值的字符串,如:

console.log(in1.attibutes.sth);     // 'sth="whatever"'

由此可以得出:

  • HTML标签中定义的属性和值会保存该DOM对象的attributes属性里面;
  • 这些attribute属性的JavaScript中的类型是Attr,而不仅仅是保存属性名和值这么简单;

那么,如果我们更改property和attribute的值会出现什么效果呢?执行如下语句:

in1.value = 'new value of prop';
console.log(in1.value);             // 'new value of prop'
console.log(in1.attributes.value);  // 'value="1"'

此时,页面中的输入栏的值变成了“new value of prop”,而propety中的value也变成了新的值,但attributes却仍然是“1”。从这里可以推断,property和attribute的同名属性的值并不是双向绑定的。

如果反过来,设置attitudes中的值,效果会怎样呢?

in1.attributes.value.value = 'new value of attr';
console.log(in1.value);             // 'new value of attr'
console.log(in1.attributes.value);  // 'new value of attr'

此时,页面中的输入栏得到更新,property中的value也发生了变化。此外,执行下面语句也会得到一样的结果

in1.attributes.value.nodeValue = 'new value of attr';

由此,可得出结论:

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


setAttribute和getAttribute

基于之前测试使用的输入框,执行如下代码:

in1.setAttribute('value', 'new attr from setAttribute');

console.log(in1.getAttribute('value'));         // 'new attr from setAttribute'
console.log(in1.value);                         // 'new attr from setAttribute'
console.log(in1.attributes.value);              // 'value=​"new attr from setAttribute"',实际是一个Attr对象

执行完setAttribute以后,就如同直接更改attributes中的同名属性;
getAttribute的结果与访问property的结果一模一样,而不会像直接访问attritudes那样返回一个Attr对象。

特殊的例子

href

然而,是不是所有标签,所有属性都维持保持这样的特性呢?下面我们看看href这个属性/特性。

首先在html中创建一个标签:

<a href='page_1.html' id='a_1'></a>

在JS脚本中执行如下代码:

console.log(a1.href);   // 'file:///D:/GitHub/JS/html/test_01/page_1.html'
console.log(a1.getAttribute('href'));   // 'page_1.html'

可以看到,property中保存的是绝对路径,而attribute中保存的是相对路径。那么,如果更改了这些值会发生什么情况呢?

更改attribute:

a1.setAttribute('href', 'page_2.html');     // 相对路径
console.log(a1.href);   // 'file:///D:/GitHub/JS/html/test_01/page_2.html'
console.log(a1.getAttribute('href'));   // 'page_2.html'

a1.setAttribute('href', '/page_3.html');    // 根目录路径
console.log(a1.href);                       // 'file:///D:/page_3.html'
console.log(a1.getAttribute('href'));       // '/page_3.html'

更改property:

a1.href = 'home.html';  // 相对路径
console.log(a1.href);   // 'file:///D:/GitHub/JS/html/test_01/home.html'
console.log(a1.getAttribute('href'));   // 'home.html'

a1.href = '/home.html'; // 根目录路径
console.log(a1.href);   // 'file:///D:/home.html'
console.log(a1.getAttribute('href'));   // '/home.html'

从这里可以发现,href是特殊的属性/特性,二者是双向绑定的,更改任意一方,都会导致另一方的的值发生改变。而且,这并不是简单的双向绑定,property中的href永远保存绝对路径,而attribute中的href则是保存相对路径。

看到这里,attribute和property的区别又多了一点,然而,这又让人变得更加疑惑了。是否还有其他类似的特殊例子呢?

id

尝试改变property中的id:

a1.id = 'new_id';
console.log(a1.id);                     // 'new_id'
console.log(a1.getAttribute('id'));     // 'new_id'

天呀,现在attribute中的id从property中的id发生了同步,数据方向变成了property <=> attribute

disabled

再来看看disabled这个属性,我们往第一个添加“disabled”特性:

<input id="in_1" value="1" sth="whatever" disabled='disabled'>  // 此时input已经被禁用了

然后执行下面的代码:

console.log(in1.disabled);      // true
in1.setAttribute('disabled', false);    // 设置attribute中的disabled,无论是false还是null都不会取消禁用
console.log(in1);               // true
console.log(in1.getAttribute('disabled'));  // 'false'

改变attributes中的disabled不会改变更改property,也不会取消输入栏的禁用效果。
如果改成下面的代码:

console.log(in1.disabled);      // true
in1.disabled = false;           // 取消禁用
console.log(in1.disabled);      // false
console.log(in1.getAttribute('disabled'));  // null,attribute中的disabled已经被移除了

又或者:

console.log(in1.disabled);      // true
in1.removeAttribute('disabled');    // 移除attribute上的disabled来取消禁用
console.log(in1.disabled);      // false
console.log(in1.getAttribute('disabled'));  // null,attribute中的disabled已经被移除了

可以发现,将property中的disabled设置为false,会移除attributes中的disabled。这样数据绑定又变成了,property<=>attribute;

所以property和attritude之间的数据绑定问题并不能单纯地以“property<-attribute”来说明。

总结

分析了这么多,对property和attribute的区别理解也更深了,在这里总结一下:

创建

  • DOM对象初始化时会创建默认的基本property;
  • 只有在HTML标签中定义的attribute才会被保存在property的attributes属性中;
  • attribute会初始化property中的同名属性,但自定义的attribute不会出现在property中;
  • attribute的值都是字符串

数据绑定

  • attributes的数据会同步到property上,然而property的更改不会改变attribute;
  • 对于value,class这样的属性/特性,数据绑定的方向是单向的,attribute->property
  • 对于id而言,数据绑定是双向的,attribute<=>property
  • 对于href而言,二者是双向绑定的property中的href永远保存绝对路径,而attribute中的href则是保存相对路径。
  • 对于disabled而言,改变attributes中的disabled不会改变更改property,也不会取消输入栏的禁用效果。当property上的disabled为false时,attribute上的disabled必定会不存在,此时数据绑定可以认为是双向的;

使用

  • 可以使用DOM的setAttribute方法来同时更改attribute和property;
  • 更改property和attribute上的任意值,都会将更新反映到HTML页面中;
  • 直接访问attributes上的值会得到一个Attr对象,而通过getAttribute方法访问则会直接得到attribute的值;
  • 大多数情况(除非有浏览器兼容性问题),jQuery.attr是通过setAttribute实现,而jQuery.prop则会直接访问DOM对象的property;
关键的两句话:
  • attribute(特性),是我们赋予某个事物的特质或对象。
  • property(属性),是早已存在的不需要外界赋予的特质。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值