在上篇据说每个大牛、小牛都应该有自己的库——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. js继承

    js继承有5种实现方式: 继承第一种方式:对象冒充 function Parent(username){ this.username = username; this.hello = function ...

  2. ubuntu 12.10 sourcelist软件更新源列表(zz)

        ubuntu 12.10正式版已经发布了,国内各大开源软件源也陆续更新了资源.今天分享一下ubuntu 12.10 软件更新源列表. 首先,备份一下ubuntu 12.04 原来的源地址列表文 ...

  3. MySQL Create Table创建表

    表的创建命令需要: 表的名称 字段名称 定义每个字段(类型.长度等) 语法 下面是通用的SQL语法用来创建MySQL表: CREATE TABLE table_name (column_name co ...

  4. list对象属性排序

    Collections.sort(list, new Comparator<ScRel>() { @Override public int compare(Object o1, Objec ...

  5. VC++ Debugger Tips[转]

    本文摘自:http://blogs.msdn.com/b/vcblog/archive/2006/08/04/689026.aspx Hi, my name is Sergey Grankin and ...

  6. 原生js-焦点图轮播

    首先是html实现页面结构及主程序 <!doctype html> <html> <head> <meta charset="utf-8" ...

  7. Notes for Studying Django

    Once you added a new application to INSTALLED_APPS, the database tables need to be updated, thus you ...

  8. [Leetcode][JAVA] Path Sum I && II

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  9. jQUery 1.9中被删除的API

    jQuery1.9删除了一些在以前版本中已经过时的api,想要把那些不够安全的.缺乏效率的.用处不大的,以及带有误导的特性统统去掉.如果你想升级你的jquery版本,但又使用了如下被删除的api的话, ...

  10. Android中的IntentService

    首先说下,其他概念:Android中的本地服务与远程服务是什么? 本地服务:LocalService 应用程序内部------startService远程服务:RemoteService androi ...