据说每个大牛、小牛都应该有自己的库——DOM处理续
在上篇据说每个大牛、小牛都应该有自己的库——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处理续的更多相关文章
- 据说每个大牛、小牛都应该有自己的库——DOM处理
这几天整理了一下思路,本来觉得DOM部分会有很多东西,但是忽然发现频繁使用的其实并不太多 class class处理部分主要有四个 hasClass:检查元素是否包含某个class addClass: ...
- 据说每个大牛、小牛都应该有自己的库——JavaScript原生对象拓展
在据说每个大牛.小牛都应该有自己的库——框架篇中我扬言要做个小牛,没想到一天没更新,小伙儿伴们就戏谑的问我,油哥是不是要太监了?其实事情是这个样子的,这不是太监的节奏,一是,关于写个自己的库的想法由来 ...
- 据说每个大牛、小牛都应该有自己的库——Event处理
今天抽时间写了一部分Event处理方面的函数愈发的觉得jQuery的优秀,自己前期的想法太粗糙,造成后面这些函数参数很多,操作很很不直观,看样子是要重构的节奏,还好小伙儿伴们安慰,架构都是改出来的.继 ...
- 据说每个大牛、小牛都应该有自己的库——Ajax
蹉跎到今天终于要写Ajax部分了,平时工作中除了选择器我用jQuery的最多的就是ajax,所以这部分在自己的框架中必不可少. XMLHttpRequest 我以为对每个使用过Ajax的人来说XMLH ...
- 让所有网站都提供API的Python库:Toapi
这是一个让所有网站都提供API的Python库.以前,我们爬取数据,然后把数据存起来,再创造一个api服务以便其他人可以访问.为此,我们还要定期更新我们的数据.这个库让这一切变得容易起来.你要做的就是 ...
- Xcode 5.1 编译模拟器以及真机都能使用的静态库
Xcode 5.1.dmg 下载地址 http://pan.baidu.com/s/1jGJpKm6 1.新建 Framework & Library 工程 我起名叫ShowInfo,下面为其 ...
- pycharm每次新建项目都要重新安装一些第三方库的解决办法(转载防删)
目前有三个解决办法,也是亲测有用的: 第一个方法:因为之前有通过pycharm的project interpreter里的+号添加过一些库,但添加的库只是指定的项目用的,如果想要用,就必须用之前的项目 ...
- 每个android项目都应该使用的android 库
http://blog.teamtreehouse.com/android-libraries-use-every-project A good developer knows to never re ...
- JavaScript原生对象拓展
JavaScript原生对象拓展 在据说每个大牛.小牛都应该有自己的库——框架篇中我扬言要做个小牛,没想到一天没更新,小伙儿伴们就戏谑的问我,油哥是不是要太监了?其实事情是这个样子的,这不是太监的节奏 ...
随机推荐
- sed笔记
sed是stream editor缩写,表示流编辑器,它是一款文本处理工具,可以配合正则表达式进行文本替换. 1.使用正则表达式匹配并进行文本中的字符串替换 *使用-i选项可以直接将替换结果应用到源文 ...
- ServiceStack.OrmLite中的一些"陷阱"(3)
前文说到如果使用多数据库(不同SQL方言)时要如何开发?其实前文(第二篇)也有“透露”到.就是直接使用库提供的OrmLiteConnection 及OrmLiteConnectionFactory(I ...
- vue 2 滚动条加载更多数据实现
解析: 判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop.clientHeight.scrollHeight. scrollTop为滚动条在Y轴上的滚动距离. clientHeigh ...
- Linux下修改mysql密码
# /etc/init.d/mysql stop# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &# mysq ...
- yii隐藏域的使用方法
<?= $form->field($model, 'time')->textInput()->hiddenInput(['value'=>time()])->lab ...
- hdu 1312(DFS)
Red and Black Tme Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 安卓奇葩问题之.so库加载不了
真是哔了狗了. 今天突然遇到一个问题:之前用第三方的密码控件,给了一个.so库文件.然后我就放在了/jniLibs/armeabi目录下. 运行,一切都很OK. 然后重点来了.N天之后的今天,突然打包 ...
- Python基于pandas的数据处理(二)
14 抽样 df.sample(10, replace = True) df.sample(3) df.sample(frac = 0.5) # 按比例抽样 df.sample(frac = 10, ...
- ubuntu安装erlang
照着园子里一篇博文安装erlang,各种错调不出来.最后发现官网有解决方案: https://www.erlang-solutions.com/downloads/download-erlang-ot ...
- OPC的理解Open Packaging Conventions
Open Packaging Conventions (OPC) 博客地址:www.cnblogs.com/icmzn OPC是一个文件容器技术.被微软创建,用来存储XML或者非XML文件结合起来的规 ...