javascript之attribute 和 property
首先看看这两个单词的英文释义(来自有道词典)。先是property:
property ['prɔpəti]
n. 性质,性能;财产;所有权
英英释义:
- any area set aside for a particular purpose “the president was concerned about the property across from the White House”
同义词:place- something owned; any tangible or intangible possession that is owned by someone “that hat is my property”; ” he is a man of property”
同义词:belongings | holding | material possession- a basic or essential attribute shared by all members of a class
- a construct whereby objects or individuals can be distinguished “self-confidence is not an endearing property”
同义词:attribute | dimension- any movable articles or objects used on the set of a play or movie
同义词:prop
重点看2、3、4条。
再看attribute:
attribute [ə'tribju:t, 'ætribju:t]
n. 属性;特质
vt. 归属;把…归于英英释义:
n.
- a construct whereby objects or individuals can be distinguished
同义词:property | dimension- an abstraction belonging to or characteristic of an entity
v.
property,attribute都作“属性”解,但是attribute更强调区别于其他事物的特质/特性,而在这篇文章中也提交到attribute是property的子集。
而在JavaScript中,property和attribute更是有明显的区别。众所周知,setAttribute是为DOM节点设置/添加属性的标准方法:
var
ele = document.getElementById();
"my_ele"
ele.setAttribute();
"title"
,"it's my element"
但很多时候我们也这样写:
ele.title = "it's my element";
如果不出什么意外,他们都运行的很好,它们似乎毫无区别?而且通常情况下我们还想获取到我们设置的“属性”,我们也很爱这样写:
alert(ele.title
);
这时候,你便会遇到问题,如果你所设置的属性属于DOM元素本身所具有的标准属性,不管是通过ele.setAttribute还是ele.title的方式设置,都能正常获取。但是如果设置的属性不是标准属性,而是自定义属性呢?
ele.setAttribute();
'mytitle'
,'test my title'
alert(ele.mytitle
);//undefined
alert(ele.getAttribute());
'mytitle'
//
ele.yourtitle ='test my title'
'your test title'
;
alert(ele.getAttribute());
'yourtitle'
//null
alert(ele.yourtitle
);//
'your test title'
通过setAttribute设置的自定义属性,只能通过标准的getAttribute方法来获取;同样通过点号方式设置的自定义属性也无法通过 标准方法getAttribute来获取。在对自定义属性的处理方式上,DOM属性的标准方法和点号方法不再具有任何关联性(上诉代码在IE6-有兼容性 问题,后面会继续介绍)。
这种设置、获取“属性”的差异性,究其根源,其实也是property与attribute的差异性所致。
通过点号设置的“属性”其实是设置的property,如上所说attribute是property的子集,那么点号设置的property自然无法通过只能获取attribute的getAttribute方法来获取。

property and attribute
照图似乎更易理解,getAttribute无法获取到不属于attribute的property也是理所应当。但是这时候你会发现另外一个问题,通过setAttribute设置的属性,同样也应该属于property,那么为何无法通过点号获取?
我们换种理解,只有标准属性才可同时使用标准方法和点号方法,而对于自定义属性,标准方法和点号方法互不干扰。

自定义属性互不干扰
那么,在JavaScript中attribute并不是property的子集,property与attribute仅存在交集,即标准属性,这样疑问都可得到合理的解释。
但在IE9-中,上诉结论并不成立。IE9-浏览器中,除了标准属性,自定义属性也是共享的,即标准方法和点号皆可读写。
成功设置的attribute都会体现在HTML上,通过outerHTML可以看到attribute都被添加到了相应的tag上,所以如果 attribute不是字符串类型数据都会调用toString()方法进行转换。但是由于IE9-中,标准属性与自定义属性不做区 分,attribute依然可以是任意类型数据,并不会调用toString()转换,非字符串attribute不会体现在HTML上,但更为严重的问 题是,这样很容易就会导致内存泄漏。所以如果不是字符串类型的自定义属性,建议使用成熟框架中的相关方法(如jQuery中的data方法)。
getAttribute与点号(.)的差异性
虽然getAttribute和点号方法都能获取标准属性,但是他们对于某些属性,获取到的值存在差异性,比如href,src,value等。
<a href="#"
id="link"
>Test Link</a>
<img src="img.png"
id="image"
/>
<input type="text"
value="enter text"
id="ipt"
/>
<script>
var
$ =function
(id
){return
document.getElementById(id
);};
alert($().getAttribute(
'link'
));
'href'
//#
alert($().href);
'link'
//fullpath/file.html#
alert($().getAttribute(
'image'
))
'src'
//img.png
alert($().src)
'image'
//fullpath/img.png
alert($().getAttribute(
'ipt'
))
'value'
//enter text
alert($().value)
'ipt'
//enter text
$().value = 5;
'ipt'
alert($().getAttribute(
'ipt'
))
'value'
//enter text
alert($().value)
'ipt'
//5
</script>
测试可发现getAttribute获取的是元素属性的字面量,而点号获取的是计算值。
更多细节可查看这篇文章:Attributes and custom properties
(待翻译:http://javascript.info/tutorial/attributes-and-custom-properties#properties)
javascript之attribute 和 property的更多相关文章
- JavaScript的attribute和property辨析
1.Attribute Attribute是HTML上设置的属性,在html中显式地设置,或者通过setAttribute()方法设置. <input type='text' id='txt' ...
- javascript中attribute和property的区别详解
DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...
- javascript DOM 操作 attribute 和 property 的区别
javascript DOM 操作 attribute 和 property 的区别 在做 URLRedirector 扩展时,注意到在使用 jquery 操作 checkbox 是否勾选时,用 at ...
- Javascript中的attribute和property分析
attribute和property这两个单词,都有属性的意思,attribute有属性.特质的意思,property则有性质,性能的意思. 首先需要明确的是,在规范中,读取和设置attribute的 ...
- jQuery的attr与prop,attribute和property区别
jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...
- attribute和property兼容性分析
上一篇文章中,详细的分析了他们的区别,请看Javascript中的attribute和property分析 这次,来详细的看下他们的兼容性,这些内容主要来自于对于jQuery(1.9.x)源代码的分析 ...
- boolean attribute(布尔值属性) attribute vs property
boolean attribute(布尔值属性) boolean attribute HTML - Why boolean attributes do not have boolean val ...
- HTML中的attribute和property
一.概述 attribute和property是常常被弄混的两个概念. 简单来说,property则是JS代码里访问的: document.getElementByTagName('my-elemen ...
- 必备技能:分清楚DOM的attribute和property
分清楚DOM的attribute和property,用JQ的时候分清楚attr,和prop方法,网上有很多大神的总结,我就不列举了.
随机推荐
- 【HDOJ】1987 Decoding
简单搜索. /* hdoj 1987 */ #include <iostream> #include <cstdio> #include <cstring> #in ...
- 2015 CCC - 01 统计数对
源:CNUOJ-0384 http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=354 题目分析:当时拿到这道题第一个想法就是排序后n^2暴 ...
- POJ-3189-Steady Cow Assignment(最大流+枚举)
题意 此题题意不太好懂.现有n头牛和b个牛棚,每个牛棚可以养的牛的数目都有一个限制c[i],表示该牛棚最多只能关c[i]头牛,每头牛对每一个牛棚都有一个喜爱值,用1到b来表示,现在要安排这些牛,使得牛 ...
- datagridview,textbox,combobox的数据绑定,数据赋值,picturebox的用法
一:datagridview数据绑定 二:textbox的数据绑定(datetimepicker) 总结: 最好还是写成双向绑定那种,不要再写出发事件了,只要在给textbox赋值就能重新绑定了,不然 ...
- 使用yum快速升级CentOS 6.5内核到 3.10.28
网上有不少升级CentOS内核的文章,如<CentOS 6.5 升级内核到 3.10.28>,大部分都是下载源码编译,有点麻烦. 在yum的ELRepo源中,有mainline(3.13. ...
- Computer Vision Algorithm Implementations
Participate in Reproducible Research General Image Processing OpenCV (C/C++ code, BSD lic) Image man ...
- kickstrt脚本for cobbler基于system-config-kickstart配置
1,由于是基于图形界面来生成ks自动安装脚本,所有图形生成ks脚本的服务器首先需要的就是有X Window System; 要是虚机的可以配个tigervnc-servere来进行操作 不说了直接上命 ...
- hdoj 1513 Palindrome【LCS+滚动数组】
Palindrome Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Java线程面试题 Top 50【转载】
不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开发职位都要求开发者精通多线程 ...
- NOI2015 程序自动分析 prog
何等水题 某神犇仿关押罪犯的写法 却写挂了 然而实际上并不需要补集之类的 #include<iostream> #include<cstring> #include<c ...