浏览器兼容问题

来源:互联网 发布:集成块查找软件 编辑:程序博客网 时间:2024/06/08 18:05
1、window.event 
IE:有window.event对象 

FF:没有window.event对象。可以通过给函数的参数传递event对象。

<input type="button" id="btn" name="btn" value="button1" onclick="getEvent(event)"/> 

getEvent = function(obj) {

// var event=window.event||obj

var event ;

var control; //  激发事件的元素

var theKey; // 激发事件的按键

// 这里判断是否为IE浏览器,只有IE浏览器才会直接使用window.event来代替当前事件对象

if (window.event) {

event = window.event;

control = event.srcElement;  

theKey = event.button; 

} else { // Firefox Chrome

    event = obj;
                    control = event.target;
                    theKey = event.which;

}

  alert(theKey);
        alert(control.attributes.getNamedItem("id").nodeValue); // 当前元素的id

}


2 . 事件注册 

//例:addEvent(window,"load",func) 
function addEvent(elem, type, fn) { 
    if (elem.attachEvent) { 
        elem.attachEvent('on' + type, fn); 
    } else if (elem.addEventListener) { 
        elem.addEventListener(type, fn, false); 
        } 
    } 
}
//例:removeEvent(window,"load",func) 
function removeEvent(elem, type, fn) { 
    if (elem.detachEvent) { 
        elem.detachEvent('on' + type, fn);
    } else if (elem.removeEventListener) { 
        elem.removeEventListener(type, fn, false); 
    } 

取消事件默认行为(例如点击一个<a>后不跳转页面而是执行一个函数) 

function eventHandler(e) { 
  e = e || window.event; 
  // 防止默认行为 
  if (e.preventDefault) { 
      e.preventDefault();//IE以外 
  } else { 
      e.returnValue = false;//IE 
      //注意:这个地方是无法用return false代替的 
      //return false只能取消元素 
  } 

阻止事件冒泡 

function myParagraphEventHandler(e) { 
    e = e || window.event; 
    if (e.stopPropagation) { 
        e.stopPropagation();//IE以外 
    } else { 
        e.cancelBubble = true;//IE 
    } 


二、getComputedStyle是?

getComputedStyle是一个可以获取当前元素所有最终使用的CSS属性值。返回的是一个CSS样式声明对象([object CSSStyleDeclaration]),只读。

getComputedStyle() gives the final used values of all the CSS properties of an element.

语法如下:

var style = window.getComputedStyle("元素", "伪类");

例如:

var dom = document.getElementById("test"),    style = window.getComputedStyle(dom , ":after");

就两个参数,大家都懂中文的,没什么好说的。只是额外提示下:Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) 之前,第二个参数“伪类”是必需的(如果不是伪类,设置为null),不过现在嘛,不是必需参数了。

三、getComputedStyle与style的区别

我们使用element.style也可以获取元素的CSS样式声明对象,但是其与getComputedStyle方法还有有一些差异的。

  1. 只读与可写
    正如上面提到的getComputedStyle方法是只读的,只能获取样式,不能设置;而element.style能读能写,能屈能伸。
  2. 获取的对象范围
    getComputedStyle方法获取的是最终应用在元素上的所有CSS属性对象(即使没有CSS代码,也会把默认的祖宗八代都显示出来);而element.style只能获取元素style属性中的CSS样式。因此对于一个光秃秃的元素<p>getComputedStyle方法返回对象中length属性值(如果有)就是190+(据我测试FF:192, IE9:195, Chrome:253, 不同环境结果可能有差异), 而element.style就是0

四、getComputedStyle与defaultView

如果我们查看jQuery源代码,会发现,其css()方法实现不是使用的window.getComputedStyle而是document.defaultView.getComputedStyle,唷?这是怎么一回事?
jQuery源码使用document.defaultView.getComputedStyle截图证明

实际上,使用defaultView基本上是没有必要的,getComputedStyle本身就存在window对象之中。根据DennisHall的说法,使用defaultView可能一是人们不太乐意在window上专门写个东西,二是让API在Java中也可用(这我不懂,忘指点~~)。

不过有个特殊情况,在FireFox3.6上不使用defaultView方法就搞不定的,就是访问框架(frame)的样式.

五、getComputedStyle兼容性

对于桌面设备:

 ChromeFirefox (Gecko)Internet ExplorerOperaSafari基本支持支持支持9支持支持伪类元素支持支持支持不支持不支持支持

对于手机设备:

 AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile基本支持支持支持WP7 Mango支持支持伪元素支持??不支持??

上面打问号的表示没有测试,是否兼容不知。如果您方便测试,欢迎将测试结果告知,这里将及时更新,并附上您的姓名,以谢您做的贡献。

我们先把注意力放在桌面设备上,可以看到,getComputedStyle方法IE6~8是不支持的,得,背了半天的媳妇,发现是孙悟空变的——郁闷了。不急,IE自有自己的一套东西。

六、getComputedStyle与currentStyle

currentStyle是IE浏览器自娱自乐的一个属性,其与element.style可以说是近亲,至少在使用形式上类似,element.currentStyle,差别在于element.currentStyle返回的是元素当前应用的最终CSS属性值(包括外链CSS文件,页面中嵌入的<style>属性等)。

因此,从作用上将,getComputedStyle方法与currentStyle属性走的很近,形式上则stylecurrentStyle走的近。不过,currentStyle属性貌似不支持伪类样式获取,这是与getComputedStyle方法的差异,也是jQuery css()方法无法体现的一点。

//zxx: 如果你只知jQuery css()方法,你是不会知道伪类样式也是可以获取的,虽然部分浏览器不支持。

例如,我们要获取一个元素的高度,可以类似下面的代码:

alert((element.currentStyle? element.currentStyle : window.getComputedStyle(element, null)).height);

您可以狠狠地点击这里:使用getComputedStyle和currentStyle 获取元素高度demo

结果FireFox下显示24px(经过计算了), 而IE浏览器下则是CSS中的2em属性值:
Firefox下显示的计算后的24px值 张鑫旭-鑫空间-鑫生活 IE9下显示的CSS中的2em值 张鑫旭-鑫空间-鑫生活

getComputedStyle方法与currentStyle属性其他具体差异还有很多,我以一个普通按钮做元素,遍历了其中靠谱的属性名和属性值,您可以狠狠地点击这里:getComputedStyle和currentStyle属性展示demo

仔细对比查看,我们可以看到不少差异,例如浮动属性,FireFox浏览器下是这个(cssFloat):
FireFox下的浮动属性名

IE7浏览器下则是styleFloat :
IE7浏览器下的styleFloat属性 张鑫旭-鑫空间-鑫生活

而IE9浏览器下则是cssFloatstyleFloat都有。

等其他N多差异。

七、getPropertyValue方法

getPropertyValue方法可以获取CSS样式申明对象上的属性值(直接属性名称),例如:

window.getComputedStyle(element, null).getPropertyValue("float");

如果我们不使用getPropertyValue方法,直接使用键值访问,其实也是可以的。但是,比如这里的的float,如果使用键值访问,则不能直接使用getComputedStyle(element, null).float,而应该是cssFloatstyleFloat,自然需要浏览器判断了,比较折腾!

使用getPropertyValue方法不必可以驼峰书写形式(不支持驼峰写法),例如:style.getPropertyValue("border-top-left-radius");

兼容性
getPropertyValue方法IE9+以及其他现代浏览器都支持,见下表:

 ChromeFirefox (Gecko)Internet ExplorerOperaSafari基本支持支持支持9支持支持

OK,一涉及到兼容性问题(IE6-8肿么办),感觉头开始微微作痛了~~,不急,IE自由一套自己的套路,就是getAttribute方法。

八、getPropertyValue和getAttribute

在老的IE浏览器(包括最新的),getAttribute方法提供了与getPropertyValue方法类似的功能,可以访问CSS样式对象的属性。用法与getPropertyValue类似:

style.getAttribute("float");

注意到没,使用getAttribute方法也不需要cssFloatstyleFloat的怪异写法与兼容性处理。不过,还是有一点差异的,就是属性名需要驼峰写法,如下:

style.getAttribute("backgroundColor");

如果不考虑IE6浏览器,貌似也是可以这么写:

style.getAttribute("background-color");

实例才是王道,您可以狠狠地点击这里:getPropertyValue和getAttribute获取背景色demo

结果FireFox下一如既往的rgb颜色返回(Chrome也是返回rgb颜色):
FireFox浏览器下一如既往的RGB颜色返回 张鑫旭-鑫空间-鑫生活

对于IE9浏览器,虽然应用的是currentStyle, 但是从结果上来讲,currentStyle返回的对象是完全支持getPropertyValue方法的。

九、getPropertyValue和getPropertyCSSValue

从长相上看getPropertyCSSValuegetPropertyValue是近亲,但实际上,getPropertyCSSValue要顽劣的多。

getPropertyCSSValue方法返回一个CSS最初值(CSSPrimitiveValue)对象(width, height, left, …)或CSS值列表(CSSValueList)对象(backgroundColor, fontSize, …),这取决于style属性值的类型。在某些特别的style属性下,其返回的是自定义对象。该自定义对象继承于CSSValue对象(就是上面所说的getComputedStyle以及currentStyle返回对象)。

getPropertyCSSValue方法兼容性不好,IE9浏览器不支持,Opera浏览器也不支持(实际支持,只是老是抛出异常)。而且,虽然FireFox中,style对象支持getPropertyCSSValue方法,但总是返回null. 因此,目前来讲,getPropertyCSSValue方法可以先不闻不问。

http://www.zhangxinxu.com/wordpress/
0 0
原创粉丝点击