attribute和property兼容性分析
上一篇文章中,详细的分析了他们的区别,请看Javascript中的attribute和property分析
这次,来详细的看下他们的兼容性,这些内容主要来自于对于jQuery(1.9.x)源代码的分析(attributes模块),首先,贴出测试的HTML代码:
<input id="username" type="text" value="lonelyclick"> <button value="abc" id="btn">def</button>
<button id="btn2">btn2222</button>
<button id="btn3">btn3</button> <div style="color:blue;" id="box">box</div> <a href="/get.do" id="baidu">baidu</a>
<img src="../../bootstrap/images/backbone.png" alt="backbone" id="backbone"> <select id="selecteddd">
<option>option1</option>
</select> <input type="checkbox" id="checkonElement">
下面开始逐一的分析jQuery中attributes模块中关于兼容性的处理:
兼容点1:
- 问题:在动态创建input是,如果先设置了值,再setAttribute type为radio的时候,input.value的值会丢失
- 浏览器:IE全系列
- 治疗方式:设置时,先将值保存起来
// $('input').attr('type','radio);
// attrHooks.type 的处理
var input = document.createElement('input');
input.value = 'ttt';
input.setAttribute('type', 'radio');
//alert(input.value); // 在IE下,弹出on 在chrome下,弹出ttt
//但是注意,这样不会出问题
var username = document.getElementById('username');
username.value = 'lonelyclick other';
username.setAttribute('type', 'radio');
//alert(username.value); //全部浏览器全部弹出lonelyclick other
//针对不兼容浏览器
function setInputRadioAttribute(input) {
var back = input.value;
input.setAltertitle('type', 'radio');
if (back) {
input.value = back;
}
}
兼容点2:
- 问题:在为button.setAttribute('value','abc')会错误的将button.innerHTML设置为abc,get亦然
- 浏览器:IE6~7
- 治疗方式:用AttributeNode进行兼容
var btn = document.getElementById('btn');
//alert(btn.getAttribute('value')); //IE6~7 弹出def,其他弹出abc
var btn2 = document.getElementById('btn2');
btn2.setAttribute('value', 'seo');
//alert(btn2.innerHTML);// IE6~7 弹出seo,其他弹出btn2222
//针对不兼容浏览器
function setButtonValueAttribute(button, value) {
var an = document.createAttribute('value');
an.value = value;
button.setAttributeNode(an);
}
function getButtonValueAttribute(button) {
var an = button.getAttributeNode('value');
return an && an.specified ? an.value : undefined;
}
var btn3 = document.getElementById('btn3');
setButtonValueAttribute(btn3, 'somethingabc');
//alert(btn3.innerHTML); // btn3
//alert(btn3.getAttribute('value')); //somethingabc
//alert(getButtonValueAttribute(btn3)); //somethingabc
兼容点3:
- 问题:element.setAttribute/getAttribute style 会出问题
- 浏览器:IE6~8
- 治疗方式:读取和设置element.style.cssText来代替setAttribute和getAttribute
var box = document.getElementById('box');
console.log(box.style); // 全部浏览器返回CSSStyleDeclaration
console.log(box.getAttribute('style')); // IE6~7 返回CSSStyleDeclaration,IE8返回COLOR:blue; 其他color:blue;
box.setAttribute('style', 'background:red;') //IE6~7不起作用,其他起作用
//针对不兼容浏览器
function setStyleAttribute(element, styleText) {
element.style.cssText = styleText + '';
}
function getStyleAttribute(element) {
return element.style.cssText;
}
兼容点4:
- 问题:在W3C下,获得href和src是绝对的属性值,但是IE自作聪明,将其补全为绝对路径
- 浏览器:IE6~7
- 治疗方式:用IE特有的另一个参数!!!,2是指href的value值,4是补全
var baidu = document.getElementById('baidu');
console.log(baidu.href); // all is http://localhost:63342/get.do
console.log(baidu.getAttribute('href')); // IE6~7返回http://localhost:63342/get.do 其他返回/get.do
var backbone = document.getElementById('backbone');
console.log(backbone.getAttribute('src'));
console.log(backbone.src);
//IE6~7 http://localhost:63342/HTML5Exp/bootstrap/images/backbone.png
//其他返回 ../../bootstrap/images/backbone.png
//针对不兼容浏览器
//http://msdn.microsoft.com/en-us/library/ms536429%28VS.85%29.aspx
console.log(baidu.getAttribute('href', 2));
console.log(backbone.getAttribute('src', 2));
console.log(baidu.getAttribute('href', 4));
console.log(backbone.getAttribute('src', 4));
兼容点5:
- 问题:在动态创建select时,加入一个option,然后取select.selected IE返回false,其他返回true
- 浏览器:IE全版本
- 治疗方式:访问一下select的selectedIndex属性,修复选择下标,如果有optgroup的话,也要访问optgroup的哦
//不会有问题
var selecteddd = document.getElementById('selecteddd');
console.log(selecteddd.options[0].selected); var select = document.createElement('select');
var option = document.createElement('option');
option.innerHTML = 'option111';
option.value = 1;
select.appendChild(option);
document.body.appendChild(select); console.log(option.selected); //IE6~7 false 其他true //针对不兼容浏览器
select.selectedIndex;
console.log(option.selected); // true
兼容点6:
- 问题:默认的radio或者checkbox的input,默认value为空字符串
- 浏览器:safair
- 治疗方式:如果有这个bug,就判断是否为'',如果为'',返回on就好了
var checkonElement = document.getElementById('checkonElement');
//alert(checkonElement.value); //safair为空字符串 其他为on
//针对不兼容浏览器
function getSafairRadioCheckboxValue(input) {
var v = input.value;
return v === '' ? 'on' : v;
}
兼容点7:
- 问题:动态创建的input,如果设置checked或者selected为true,就不起作用的
- 浏览器:IE6~7
- 治疗方式:用defaultChecked和defaultSelected替换之
var defaultCheckedInput = document.createElement('input');
defaultCheckedInput.type = 'checkbox';
defaultCheckedInput.checked = true; //无效,在浏览器上可以看到,没有被选中
document.body.appendChild(defaultCheckedInput);
//针对不兼容浏览器
var defaultCheckedInput = document.createElement('input');
defaultCheckedInput.type = 'checkbox';
defaultCheckedInput.defaultChecked = true; //被选中
document.body.appendChild(defaultCheckedInput);
这一部分是为jQuery的源码分析做参考,请看:jQuery.attributes源码分析(attr/prop/val/class)
参考
- http://www.cnblogs.com/rubylouvre/p/3524113.html
- http://www.cnblogs.com/snandy/archive/2012/05/06/2473936.html
- http://www.cnblogs.com/rubylouvre/p/3524113.html
- http://www.cnblogs.com/rubylouvre/archive/2009/12/07/1618182.html
- http://www.w3help.org/zh-cn/causes/SD2021
- http://www.jb51.net/article/30389.htm
- http://www.jb51.net/article/39485.htm
- http://www.cnblogs.com/top5/archive/2011/07/13/2105260.html
attribute和property兼容性分析的更多相关文章
- Javascript中的attribute和property分析
attribute和property这两个单词,都有属性的意思,attribute有属性.特质的意思,property则有性质,性能的意思. 首先需要明确的是,在规范中,读取和设置attribute的 ...
- jQuery的attr与prop,attribute和property区别
jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...
- javascript之attribute 和 property
首先看看这两个单词的英文释义(来自有道词典).先是property: property ['prɔpəti] n. 性质,性能:财产:所有权 英英释义: any area set aside for ...
- javascript DOM 操作 attribute 和 property 的区别
javascript DOM 操作 attribute 和 property 的区别 在做 URLRedirector 扩展时,注意到在使用 jquery 操作 checkbox 是否勾选时,用 at ...
- JavaScript的attribute和property辨析
1.Attribute Attribute是HTML上设置的属性,在html中显式地设置,或者通过setAttribute()方法设置. <input type='text' id='txt' ...
- 必备技能:分清楚DOM的attribute和property
分清楚DOM的attribute和property,用JQ的时候分清楚attr,和prop方法,网上有很多大神的总结,我就不列举了.
- Attribute和Property
有时很容易对Attribute和Property混淆,因为中文翻译都是“属性”来解释的.其实这两个表达的不是一个层面的东西. Property属于面向对象理论范畴,在使用面向对象思想编程的时候,常常需 ...
- boolean attribute(布尔值属性) attribute vs property
boolean attribute(布尔值属性) boolean attribute HTML - Why boolean attributes do not have boolean val ...
- 详解JS中DOM 元素的 attribute 和 property 属性
一.'表亲戚':attribute和property 为什么称attribute和property为'表亲戚'呢?因为他们既有共同处,也有不同点. attribute 是 dom 元素在文档中作为 h ...
随机推荐
- css中关于transform、transition、animate的区别
写动画经常会用到这几个属性,他们之间有什么区别呢? 1.transform 每每演示transform属性的,看起来好像都是带动画.这使得小部分直觉化思维的人(包括我)认为transform属性是动画 ...
- ORACLE 查询表定义
很多文章使用DESC tablename查看,这是查看的表结构,不是表定义. 如下: 1.set long 99999; --增大输出缓冲区 2.SELECT dbms_metadata.get_d ...
- 回溯算法————n皇后、素数串
回溯就是算法是搜索算法中一种控制策略,是一个逐个试探的过程.在试探的过程中,如果遇到错误的选择,就会回到上一步继续选择下一种走法,一步一步的进行直到找到解或者证明无解为止. 如下是一个经典回溯问题n皇 ...
- .Net之垃圾回收算法
垃圾回收器检测托管堆中是否有应用程序不在使用的任何对象,如果一次垃圾回收之后,堆栈没有可用的内存,new操作符将会抛出OutOfMemoryException(内存溢出). 每一个应用程序都包含一组根 ...
- Android Bitmaps缓存
Android 开发中,bitmap是引起内存泄漏的罪魁祸首,关于bitmap的加载,缓存策略,官方已经给了很详细的方法: 缓存之Memory Cache: 缓存的策略,是利用应用程序的分配的内存拿出 ...
- javascript 数据结构和算法读书笔记 > 第一章 javascript的编程环境和模型
1.变量的声明和初始化 必须使用关键字 var,后跟变量名,后面还可以跟一个赋值表达式. var name; var age = 5; var str = 'hello'; var flg = fal ...
- mysql 查询重复的(不区分大小写)数据的SQL优化
在mysql中查询不区分大小写重复的数据,往往会用到子查询,并在子查询中使用upper函数来将条件转化为大写.如: select * from staticcatalogue WHERE UPPER( ...
- 安装ecshop出错
在安装Ecshop的时候,遇到两个问题: 1.Strict Standards: Non-static method cls_image::gd_version() should not be cal ...
- data stage走起
如题,希望以后可以找到相应的工作.(已经工作3年以上了)
- CSS自学笔记(12):CSS3文字特效
在CSS3中新增了多个文本属性,同样有了这些属性我们在进行问题特效处理时,就尽可能少的用到其他软件去制作特效文字了. 在以前使用CSS进行web开发的时候,必须使用计算机上安装好的字体,如果有些用户的 ...