在上篇据说每个大牛、小牛都应该有自己的库——DOM处理最后剩下attr()和css()方法没有处理,因为这两个方法当时并不自计划中,是写着写着突然想到的,一时间没有特别好的思路,当时已十一点多了,就去睡了。没想到啊没想到接下来的一星期简直是噩梦,每天加班回家都十一点,今天有时间赶紧补上。

property与attribute

之前说了这两个方法是仿照jQuery的,看了一下jQuery的源码,发现从1.6后jQuery多了一个prop()方法,做的功能却和attr()很相似,看了很多资料才明白prop是在解决什么问题。

property和attribute都可以翻译为属性,不过为了区别一般把property翻译为特性,而在JavaScript中,property和attribute的区别不止这么简单。setAttribute是为DOM节点设置/添加属性的标准方法,我们一般会这么用

var e = document.getElementById('ck');
e.setAttribute('title', 'test');

也可以这么用

var e = document.getElementById('ck');
e.title = 'test';

无论怎么设,在读取的时候这么用

alert(e.getAttribute('title'));
alert(e.title);

对attribute设定/读取值使用setAttribute/getAttribute,property使用.操作符,两种用法看似毫无区别,但是我们也经常设置元素的class,想要得到预期结果得这么写

e.setAttribute('class', 'test');
e.className = 'test';

同样都是对class操作,使用attribute的key是class,property却是className,其读取的结果也不一定相同(input 的checkbox)

console.log(e.getAttribute('checked')); //checked
console.log(e.checked); //true

DOM对象大部分的内置property都有对应的名字的attribute(名字也可能不同,比如上面的class),对于自定义的属性双发互不干扰(IE9以下版本还是共享的)

e.setAttribute('customizeProperty', 'attribute');
e.customizeProperty = 'property';
console.log(e.getAttribute('customizeProperty')); //attribute
console.log(e.customizeProperty); //property

看起来似乎很清楚了,看个例子

<input id="ck" type="checkbox" checked="checked" />
<script type="text/javascript">
var e = document.getElementById('ck'); console.log(e.getAttribute('checked')); //checked
console.log(e.checked); //true e.checked = false;
console.log(e.getAttribute('checked')); //checked
console.log(e.checked); //false e.setAttribute('checked', 'checked');
console.log(e.getAttribute('checked')); //checked
console.log(e.checked); //false </script>

这是怎么个情况,不是内置属性是共享的吗,怎么互不干扰了?这是因为一些Boolean类型的属性(如checked, selected, disabled等)比较特殊,其attribute只保留初始值(默认值), property才是当前最新的状态值。一个默认勾选的checkbox,当在页面去除勾选的时候,checked这个property已由true变为false,而checked这个attribute仍然保持“checked”这个初始值。

attr()

这都和attr()有神马关系,说上面的原因是既然了解了property和attribute的不同,那么自己的库干脆也像jQuery把property和attribute分开处理。自己写了很多,总是有这样那样的问题,看到了大神John Resig的处理方法后,豁然开朗,这两个方法是可以写成结合体的,无耻的简单改造了一下抄过来,注释写的都这么经典没舍得删

attr: function (elem, name, value) {
// Are we setting a value?
if (typeof name == 'object') {
for (n in name) {
attr(elem, n, name[n]);
}
}
else if (value !== undefined) {
// Make sure the element has the ability to set an attribute
if (typeof elem.setAttribute !== "undefined") {
// If the user is setting the value to false
if (value === false) {
// Completely remove the attribute
elem.removeAttribute(name); // Otherwise set the attribute value
} else {
// If the user is setting the value to true,
// Set it equal to the name of the attribute
// (handles boolean attributes nicely)
elem.setAttribute(name, value === true ? name : value);
} // If it doesn't, then we're likely dealing with window or document
// (or some other object entirely)
} else {
elem[name] = value;
} // Otherwise we're getting an attribute value
// Check to see if the appropriate method exists
// Also don't use getAttribute if a boolean property exists
} else if (typeof elem.getAttribute !== "undefined" && typeof elem[name] !== "boolean") {
return elem.getAttribute(name); // If no getAttribute method is present, or if we
// wish to access the boolean property instead of the
// attribute, then we fallback to the DOM object property
} else {
return elem[name];
}
}

css()

css()方法就写不了jQuery那么强大了,基本没有做错误处理,所以使用的时候必须保证传入的属性名称和值是正确的,同时只能传入简单的属性名,而不能是-moz-等类似的

css:function(ele,name,value){
if(typeof name=='object'){
for(n in name){
ssLib.css(ele,n,name[n]);
}
}else if(typeof value!='undefined'){
ele.style[_parseStyleName(name)]=value;
}else{
return ele.style[_parseStyleName(name)];
}
}

参考

http://ejohn.org/blog/jquery-16-and-attr/(大神John Resig的博客)

据说每个大牛、小牛都应该有自己的库——DOM处理续的更多相关文章

  1. 据说每个大牛、小牛都应该有自己的库——DOM处理

    这几天整理了一下思路,本来觉得DOM部分会有很多东西,但是忽然发现频繁使用的其实并不太多 class class处理部分主要有四个 hasClass:检查元素是否包含某个class addClass: ...

  2. 据说每个大牛、小牛都应该有自己的库——JavaScript原生对象拓展

    在据说每个大牛.小牛都应该有自己的库——框架篇中我扬言要做个小牛,没想到一天没更新,小伙儿伴们就戏谑的问我,油哥是不是要太监了?其实事情是这个样子的,这不是太监的节奏,一是,关于写个自己的库的想法由来 ...

  3. 据说每个大牛、小牛都应该有自己的库——Event处理

    今天抽时间写了一部分Event处理方面的函数愈发的觉得jQuery的优秀,自己前期的想法太粗糙,造成后面这些函数参数很多,操作很很不直观,看样子是要重构的节奏,还好小伙儿伴们安慰,架构都是改出来的.继 ...

  4. 据说每个大牛、小牛都应该有自己的库——Ajax

    蹉跎到今天终于要写Ajax部分了,平时工作中除了选择器我用jQuery的最多的就是ajax,所以这部分在自己的框架中必不可少. XMLHttpRequest 我以为对每个使用过Ajax的人来说XMLH ...

  5. 让所有网站都提供API的Python库:Toapi

    这是一个让所有网站都提供API的Python库.以前,我们爬取数据,然后把数据存起来,再创造一个api服务以便其他人可以访问.为此,我们还要定期更新我们的数据.这个库让这一切变得容易起来.你要做的就是 ...

  6. Xcode 5.1 编译模拟器以及真机都能使用的静态库

    Xcode 5.1.dmg 下载地址 http://pan.baidu.com/s/1jGJpKm6 1.新建 Framework & Library 工程 我起名叫ShowInfo,下面为其 ...

  7. pycharm每次新建项目都要重新安装一些第三方库的解决办法(转载防删)

    目前有三个解决办法,也是亲测有用的: 第一个方法:因为之前有通过pycharm的project interpreter里的+号添加过一些库,但添加的库只是指定的项目用的,如果想要用,就必须用之前的项目 ...

  8. 每个android项目都应该使用的android 库

    http://blog.teamtreehouse.com/android-libraries-use-every-project A good developer knows to never re ...

  9. JavaScript原生对象拓展

    JavaScript原生对象拓展 在据说每个大牛.小牛都应该有自己的库——框架篇中我扬言要做个小牛,没想到一天没更新,小伙儿伴们就戏谑的问我,油哥是不是要太监了?其实事情是这个样子的,这不是太监的节奏 ...

随机推荐

  1. less 里面 opacity的写法

    今天写了个opacity, 竟然less编译不过,上网搜了一个写法 .opacity (@opacity) { @opacityPercentage: @opacity * 100; opacity: ...

  2. css3之多列

                                             

  3. jekyll安装的斗智斗勇

    jekyll---将纯文本转化为静态网站和博客,GitHub Pages 可以运行 Jekyll,你很简单就可以完全免费的在 GitHub 上发布网站. 小白安装jekyll时的若干问题,有错误欢迎指 ...

  4. Linux下连接MSSQL之安装FreeTDS

    $ tsql -H MSSQL服务器服务IP -p 1433 -U MSSQL服务器登陆帐号 -P MSSQL服务器登陆密码http://www.cnblogs.com/ilovexiao/p/355 ...

  5. Encapsulation、Inheritance、Polymorphism

    public class SoldierDemo { public static void main(String[] args) { /*Soldier test1=new Army("张 ...

  6. JS-随机排序

    var arr = [ 1,2,3,4,5,6,7,8 ];arr.sort(function ( a, b ) {    return Math.random() - 0.5;});alert( a ...

  7. oracle DBMS_LOCK.SLEEP()的使用

    create or replace procedure times isii positive:=1;beginloop dbms_lock.sleep(1);dbms_output.put_line ...

  8. 使用git svn clone迁移svn仓库

    使用git svn clone迁移svn仓库 clone命令可以指定很多参数,主要用到这些,你也可以使用git svn help查看完整的参数列表. git svn clone https://172 ...

  9. 【2016.3.30项目技术记录】]VS2010自动生成MFC单文档框架程序的修改:去除属性框,在CViewTree类中添加鼠标单击响应

    转自http://blog.csdn.net/yanfeiouc2009/archive/2010/06/07/5653360.aspx 手头上有个东西要用到单文档,由于想省事,直接用VS2010做了 ...

  10. c1ctf2016 wp

    web: 1.web萌新福利 没啥好说的,右键查看源码得key 2.you are not admin 一看题目,就想到http头修改,常见的x-forwarded-for,referer,host, ...