上一篇文章中,详细的分析了他们的区别,请看Javascript中的attribute和property分析

这次,来详细的看下他们的兼容性,这些内容主要来自于对于jQuery(1.9.x)源代码的分析(attributes模块),首先,贴出测试的HTML代码:

  1. <input id="username" type="text" value="lonelyclick">
  2.  
  3. <button value="abc" id="btn">def</button>
  4. <button id="btn2">btn2222</button>
  5. <button id="btn3">btn3</button>
  6.  
  7. <div style="color:blue;" id="box">box</div>
  8.  
  9. <a href="/get.do" id="baidu">baidu</a>
  10. <img src="../../bootstrap/images/backbone.png" alt="backbone" id="backbone">
  11.  
  12. <select id="selecteddd">
  13. <option>option1</option>
  14. </select>
  15.  
  16. <input type="checkbox" id="checkonElement">

下面开始逐一的分析jQuery中attributes模块中关于兼容性的处理:

兼容点1:

  • 问题:在动态创建input是,如果先设置了值,再setAttribute type为radio的时候,input.value的值会丢失
  • 浏览器:IE全系列
  • 治疗方式:设置时,先将值保存起来
  1. // $('input').attr('type','radio);
  2. // attrHooks.type 的处理
  3. var input = document.createElement('input');
  4. input.value = 'ttt';
  5. input.setAttribute('type', 'radio');
  6. //alert(input.value); // 在IE下,弹出on 在chrome下,弹出ttt
  7.  
  8. //但是注意,这样不会出问题
  9. var username = document.getElementById('username');
  10. username.value = 'lonelyclick other';
  11. username.setAttribute('type', 'radio');
  12. //alert(username.value); //全部浏览器全部弹出lonelyclick other
  13.  
  14. //针对不兼容浏览器
  15. function setInputRadioAttribute(input) {
  16. var back = input.value;
  17. input.setAltertitle('type', 'radio');
  18. if (back) {
  19. input.value = back;
  20. }
  21. }

兼容点2:

  • 问题:在为button.setAttribute('value','abc')会错误的将button.innerHTML设置为abc,get亦然
  • 浏览器:IE6~7
  • 治疗方式:用AttributeNode进行兼容
  1. var btn = document.getElementById('btn');
  2. //alert(btn.getAttribute('value')); //IE6~7 弹出def,其他弹出abc
  3.  
  4. var btn2 = document.getElementById('btn2');
  5. btn2.setAttribute('value', 'seo');
  6. //alert(btn2.innerHTML);// IE6~7 弹出seo,其他弹出btn2222
  7.  
  8. //针对不兼容浏览器
  9. function setButtonValueAttribute(button, value) {
  10. var an = document.createAttribute('value');
  11. an.value = value;
  12. button.setAttributeNode(an);
  13. }
  14.  
  15. function getButtonValueAttribute(button) {
  16. var an = button.getAttributeNode('value');
  17. return an && an.specified ? an.value : undefined;
  18. }
  19.  
  20. var btn3 = document.getElementById('btn3');
  21.  
  22. setButtonValueAttribute(btn3, 'somethingabc');
  23. //alert(btn3.innerHTML); // btn3
  24. //alert(btn3.getAttribute('value')); //somethingabc
  25. //alert(getButtonValueAttribute(btn3)); //somethingabc

兼容点3:

  • 问题:element.setAttribute/getAttribute style 会出问题
  • 浏览器:IE6~8
  • 治疗方式:读取和设置element.style.cssText来代替setAttribute和getAttribute
  1. var box = document.getElementById('box');
  2. console.log(box.style); // 全部浏览器返回CSSStyleDeclaration
  3. console.log(box.getAttribute('style')); // IE6~7 返回CSSStyleDeclaration,IE8返回COLOR:blue; 其他color:blue;
  4. box.setAttribute('style', 'background:red;') //IE6~7不起作用,其他起作用
  5.  
  6. //针对不兼容浏览器
  7. function setStyleAttribute(element, styleText) {
  8. element.style.cssText = styleText + '';
  9. }
  10.  
  11. function getStyleAttribute(element) {
  12. return element.style.cssText;
  13. }

兼容点4:

  • 问题:在W3C下,获得href和src是绝对的属性值,但是IE自作聪明,将其补全为绝对路径
  • 浏览器:IE6~7
  • 治疗方式:用IE特有的另一个参数!!!,2是指href的value值,4是补全
  1. var baidu = document.getElementById('baidu');
  2. console.log(baidu.href); // all is http://localhost:63342/get.do
  3. console.log(baidu.getAttribute('href')); // IE6~7返回http://localhost:63342/get.do 其他返回/get.do
  4.  
  5. var backbone = document.getElementById('backbone');
  6. console.log(backbone.getAttribute('src'));
  7. console.log(backbone.src);
  8. //IE6~7 http://localhost:63342/HTML5Exp/bootstrap/images/backbone.png
  9. //其他返回 ../../bootstrap/images/backbone.png
  10.  
  11. //针对不兼容浏览器
  12. //http://msdn.microsoft.com/en-us/library/ms536429%28VS.85%29.aspx
  13. console.log(baidu.getAttribute('href', 2));
  14. console.log(backbone.getAttribute('src', 2));
  15.  
  16. console.log(baidu.getAttribute('href', 4));
  17. console.log(backbone.getAttribute('src', 4));

兼容点5:

  • 问题:在动态创建select时,加入一个option,然后取select.selected IE返回false,其他返回true
  • 浏览器:IE全版本
  • 治疗方式:访问一下select的selectedIndex属性,修复选择下标,如果有optgroup的话,也要访问optgroup的哦
  1. //不会有问题
  2. var selecteddd = document.getElementById('selecteddd');
  3. console.log(selecteddd.options[0].selected);
  4.  
  5. var select = document.createElement('select');
  6. var option = document.createElement('option');
  7. option.innerHTML = 'option111';
  8. option.value = 1;
  9. select.appendChild(option);
  10. document.body.appendChild(select);
  11.  
  12. console.log(option.selected); //IE6~7 false 其他true
  13.  
  14. //针对不兼容浏览器
  15. select.selectedIndex;
  16. console.log(option.selected); // true

兼容点6:

  • 问题:默认的radio或者checkbox的input,默认value为空字符串
  • 浏览器:safair
  • 治疗方式:如果有这个bug,就判断是否为'',如果为'',返回on就好了
  1. var checkonElement = document.getElementById('checkonElement');
  2. //alert(checkonElement.value); //safair为空字符串 其他为on
  3.  
  4. //针对不兼容浏览器
  5. function getSafairRadioCheckboxValue(input) {
  6. var v = input.value;
  7. return v === '' ? 'on' : v;
  8. }

兼容点7:

  • 问题:动态创建的input,如果设置checked或者selected为true,就不起作用的
  • 浏览器:IE6~7
  • 治疗方式:用defaultChecked和defaultSelected替换之
  1. var defaultCheckedInput = document.createElement('input');
  2. defaultCheckedInput.type = 'checkbox';
  3. defaultCheckedInput.checked = true; //无效,在浏览器上可以看到,没有被选中
  4. document.body.appendChild(defaultCheckedInput);
  5.  
  6. //针对不兼容浏览器
  7. var defaultCheckedInput = document.createElement('input');
  8. defaultCheckedInput.type = 'checkbox';
  9. defaultCheckedInput.defaultChecked = true; //被选中
  10. 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兼容性分析的更多相关文章

  1. Javascript中的attribute和property分析

    attribute和property这两个单词,都有属性的意思,attribute有属性.特质的意思,property则有性质,性能的意思. 首先需要明确的是,在规范中,读取和设置attribute的 ...

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

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

  3. javascript之attribute 和 property

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

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

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

  5. JavaScript的attribute和property辨析

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

  6. 必备技能:分清楚DOM的attribute和property

    分清楚DOM的attribute和property,用JQ的时候分清楚attr,和prop方法,网上有很多大神的总结,我就不列举了.

  7. Attribute和Property

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

  8. boolean attribute(布尔值属性) attribute vs property

    boolean attribute(布尔值属性) boolean attribute     HTML - Why boolean attributes do not have boolean val ...

  9. 详解JS中DOM 元素的 attribute 和 property 属性

    一.'表亲戚':attribute和property 为什么称attribute和property为'表亲戚'呢?因为他们既有共同处,也有不同点. attribute 是 dom 元素在文档中作为 h ...

随机推荐

  1. csss3 2D转换

    CSS3 转换 通过 CSS3 转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. 它如何工作? 转换是使元素改变形状.尺寸和位置的一种效果. 您可以使用 2D 或 3D 转换来转换您的元素. 浏 ...

  2. css3实现图片遮罩效果鼠标hover以后出现文字

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 必须知道的ADO.NET 数据库连接池

    http://www.cnblogs.com/liuhaorain/archive/2012/02/19/2353110.html 题外话 通过前几章的学习,不知道大家对ADO.NET有一定的了解了没 ...

  4. Oracle文章中常用数据表的描述

    desc stud; 名称   空值       类型           ---- -------- ------------ ID   NOT NULL NUMBER(38)   NAME     ...

  5. 【IOS学习基础】内存管理

    1.内存几大区域 1> 栈区:局部变量(基本数据类型.指针变量). 2> 堆区:程序运行的过程中动态分配的存储空间(创建的对象). 3> BSS段:没有初始化的全局变量和静态变量. ...

  6. Netty详解

    Netty详解  http://blog.csdn.net/suifeng3051/article/category/2161821

  7. 一个可以拓展的垂直多级导航栏 Demo

    大四党忙忙碌碌找工作,博客荒废久矣,可谓:终日昏昏醉梦间,忽闻春尽强登山.因过竹院逢僧话,偷得浮生半日闲. 这是个垂直的导航栏,可以有无限多层的子级菜单,看代码注释就够了: <!DOCTYPE ...

  8. 关于 java.io.IOException: open failed: EACCES (Permission denied)

    今天解决了一个问题,不得不来和大家分享.就是关于 java.io.IOException: open failed: EACCES (Permission denied)的问题,网上也有很多人把这个问 ...

  9. Linq to sql 操作

    1.往数据库添加数据 NorthwindDataContext abc = new NorthwindDataContext(); abc.Log = Console.Out; User a = ne ...

  10. Linux PCI网卡驱动的详细分析

    学习应该是一个先把问题简单化,在把问题复杂化的过程.一开始就着手处理复杂的问题,难免让人有心惊胆颤,捉襟见肘的感觉.读Linux网卡驱动也是一 样.那长长的源码夹杂着那些我们陌生的变量和符号,望而生畏 ...