• Javascript不同浏览器差异及兼容方法


    原文链接:http://caibaojian.com/js-ie-different-from-firefox.html

    javascript的各种兼容就是为了解决不同浏览器的差异性,了解其中的差异能够帮助你更快的解决问题,提高代码的使用质量,编写更优秀的javascript代码。

    1.window.event

    表示当前的时间对象,IE有这个对象,FF没有,FF通过给事件处理函数传递事件对象

    2.获取事件源

    IE用srcElement获取事件源,而FF用target获取事件源

    以上两个兼容通常会这么写:

    var evt = e||event;
    
    var el = evt.srcTarget || evt.srcElement;

    3.添加、去除事件

    4.获取标签的自定义属性

    IE:div1.value或div1['value']

    FF:可用div1.getAttribute("value")

    5.document.getElemntByName()和document.all[name]

    IE不可以,

    FF可以

    6.input.type的属性

    7.IE支持innerText、outerHTML

    FF:支持textContent

    8.窗口的位置

    IE、chrome、safari:支持使用window.screenLeft和window.screenTop

    IE8以上、chrome、safari、firefox:支持使用window.screenX和window.screenY

    兼容代码可以使用下面这段代码:

    var leftX = typeof window.screenLeft == 'number' ? window.screenLeft : window.screenX;
    
    ver topY = typeof window.screenTop == 'number' ? window.screenTop : window.screenY;
    

    9.窗口的大小

    firefox、chrome、IE9和safari:window.innerWidth和window.innerHeight

    IE系列:document.body.clientWidth和document.body.clientHeight

    不是IE6:document.documentElement.clientWidth和document.documentElement.clientHeight

    兼容代码可以这样子写

    //code from http://caibaojian.com/js-ie-different-from-firefox.html
    var width = window.innerWidth;
    
    var height = window.innerHeight;
    
    if(typeof width != 'number'){
    
    if(document.compatMode == 'CSS1Compat'){
    
    width = document.documentElement.clientWidth;
    
    height = document.docuementElement.clientHeight;
    
    }else{
    
    width = document.body.clientWidth;
    
    height = document.body.clientHeight;
    
    }
    

    以上内容参考网络,由于本人学习的javascript知识还比较少,暂时不认识,所以提前学习,以便以后遇到能够快速的识别。这篇文章后面将会继续更新添加。

  • 相关阅读:
    【转帖】asp.net mvc与webform区别
    [学习jquery]深入了解jquery(1)jquery对象
    windows ce 4.2/5.0/6.0/windows mobile设备直接连接PC端的SQLserver
    dotNet dispose 和 close的区别
    简易快速理解 ERP
    互联网盈利模式77种创新 [转]
    软件开发的基础知识[1]
    什么是 SHTML
    ASP.NET 2.0:弃用 DataGrid 吧,有新的网格控件了![msdn]
    主页制作五十式[好帖就要转]
  • 原文地址:https://www.cnblogs.com/xp1056/p/5825522.html
Copyright © 2020-2023  润新知