在前阵子看JQuery源码中,attr()的简单理解是调用了element.getAttribute()和element.setAttribute()方法,removeAttr()简单而言是调用element.removeAttribute(),而prop()简单理解是element.xxx方式存取属性,removeProp()是通过delete element.xxx方式删除。

attribute与property都是属性的意思。那么有何区别呢?

对于这个问题,今天问了好几个群,也找到一些文章(感谢他们~~)

主要是看了下面几篇:

http://gxxsite.com/content/view/id/135.html

http://nuysoft.iteye.com/blog/1172122

http://www.tuicool.com/articles/3uuQRr6

http://www.web-tinker.com/article/20115.html

http://www.cnblogs.com/aaronjs/p/3387906.html

http://www.w3help.org/zh-cn/causes/SD9006

总结如下

attribute  property 举例
HTML属性 DOM节点对象属性  
返回初始化的值 返回当前的值
//<input id="test" type="text"/>
var test=document.getElementById("test");
test.getAttribute("required")
//null
test.required
//false

比如获取当前文本框的value

返回字符串 可以返回多种格式,可以是字符串,也可以是对象
 //<input id="test" type="text" style="color:#666"/>

var test=document.getElementById("test"); test.style

//CSSStyleDeclaration {0: "color", parentRule: null, length: 1, 
cssText: "color: rgb(102, 102, 102);", alignContent: "", alignItems: ""…}
test.getAttribute("style")
//"color:#666"
返回值与HTML代码中的属性值相同 返回值与HTML代码中的属性值可能不同
//<a id="test" href="/aaa.html">aaaaa</a>
var test=document.getElementById("test");
test.getAttribute("href")
//"/aaa.html"
test.href
//"file:///C:/aaa.html"
还比如checked属性
 可以获取HTML代码中自定义的属性  只能获取原生的属性值
//<input id="test" type="text" custom="text"/>

var test=document.getElementById("test");

test.getAttribute("custom")
//"text"
test.custom
//undefined
设值时DOM树结构变了 设值时DOM树结构不变
//<input id="test" type="text" value="10"/>
var test=document.getElementById("test"); test.value=20;
//<input id="test" type="text" value="10">
test.setAttribute("value",20)
//<input id="test" type="text" value="20">
     

其他:

1、element.className与 element.getAttribute("class")

2、element.for (获取不到)与 element.getAttribute("for")

3、element.onclick 与 element.getAttribute("onclick") (获取不到)
4、element.nodeName

5、属性值在浏览器之间的差异,举例:

//<input id="test" type="checkbox" checked="checked" />

alert(document.getElementById("test").checked);
//true(Chrome)
//true(IE7) alert(document.getElementById("test").getAttribute("checked"));
//checked(Chrome)
//true(IE7)

6、关于jq

①性能:prop>data>attr

attribute与property区别总结的更多相关文章

  1. jQuery的attr与prop,attribute和property区别

    jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...

  2. javascript DOM 操作 attribute 和 property 的区别

    javascript DOM 操作 attribute 和 property 的区别 在做 URLRedirector 扩展时,注意到在使用 jquery 操作 checkbox 是否勾选时,用 at ...

  3. attribute和property的区别

    DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...

  4. javascript中attribute和property的区别详解

    DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...

  5. JavaScript的attribute和property辨析

    1.Attribute Attribute是HTML上设置的属性,在html中显式地设置,或者通过setAttribute()方法设置. <input type='text' id='txt' ...

  6. Attribute和Property

    有时很容易对Attribute和Property混淆,因为中文翻译都是“属性”来解释的.其实这两个表达的不是一个层面的东西. Property属于面向对象理论范畴,在使用面向对象思想编程的时候,常常需 ...

  7. javascript之attribute 和 property

    首先看看这两个单词的英文释义(来自有道词典).先是property: property ['prɔpəti] n. 性质,性能:财产:所有权 英英释义: any area set aside for ...

  8. attribute和property兼容性分析

    上一篇文章中,详细的分析了他们的区别,请看Javascript中的attribute和property分析 这次,来详细的看下他们的兼容性,这些内容主要来自于对于jQuery(1.9.x)源代码的分析 ...

  9. C#中Attribute和Property

    XAML是XML派生而来的语言,所以很多XML中的概念在XAML中是通用的. 为了表示同类标签中的某个标签与众不同,可以给它的特征(Attribute)赋值,为特征值赋值的语法如下: 非空标签:< ...

随机推荐

  1. 基于BASYS2的VHDL程序——数字钟(改进版)

    扩展到时分秒.加了入调时电路,但不知道为什么有两个按键不好使.而且不知道以何种方式假如按键消抖电路,因为加入后会多个时钟控制一个信号,物理不可实现.调试电路待解决.还有,四个数目管中间的那两个圆点怎么 ...

  2. python生成图片

    # -*- coding:utf-8 -*- from pylab import * figure(1,figsize=(6,6)) ax = axes([0.1,0.1,0.8,0.8]) frac ...

  3. codevs 2456栅栏

    传送门 2456 栅栏 2006年省队选拔赛四川  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Maste   题目描述 Description 农夫约翰打算建立一个栅 ...

  4. HDU2203(KMP入门题)

    亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. css3 position:sticky

    最近在写一个小程序,项目中遇到一个需求:页面滚动到tab切换菜单时,菜单fixed到页面顶部: 实现方法: 使用小程序的onPageScroll事件,滚动到指定位置添加fixed样式: bug1:获取 ...

  6. 开发板启动时,内核打印出"can't access tty,job control turned off"

    启动后的最后一行提示can't access tty,job control turned off, 这说明没有进入到控制台,原因就在于文件系统的/etc/inittab 这个文件里有问题 vi /e ...

  7. Outlook 开发

    转自:http://www.cnblogs.com/madebychina/archive/2011/09/20/madebychina_2.html C#使用如下代码调用Outlook2003发送邮 ...

  8. opencv MatExpr MatOp

    opencv提供了很多Mat的操作,其中涉及到两个重要的类:MatOp和MatExpr C++: MatExpr abs(const Mat& m) C++: void absdiff(Inp ...

  9. HDU - 2612 Find a way 双起点bfs(路径可重叠:两个队列分别跑)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. SQL Server知识详解

    1.SET NOCOUNT ON的作用: 作用:阻止在结果集中返回显示受T-SQL语句或则usp影响的行计数信息. 语法:SET NOCOUNT {ON | OFF} 详解:当SET ONCOUNT ...