ie兼容火狐

来源:互联网 发布:虚拟专用网络端口号 编辑:程序博客网 时间:2024/04/19 23:07

 

 

1.       火狐中所有使用event相关的代码无法使用.

eventIE封装过的全局对象,控制所有事的事件,但是这个对象在火狐中不存在,所以在火狐中无法使用,特别是event.keyCode也是无法取得的。

解决方法:

下面这段JS重新封装了EVENT对象,直接在页面引用,原先的events代码均可试用于火狐:

<script>

/*firefox----这段js重新封装了event对象,经验证可以在火狐下支持!----*/

    function __firefox(){

        HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);

        window.constructor.prototype.__defineGetter__("event", __window_event);

        Event.prototype.__defineGetter__("srcElement", __event_srcElement);

    }

    function __element_style(){

        return this.style;

    }

    function __window_event(){

        return __window_event_constructor();

    }

    function __event_srcElement(){

        return this.target;

    }

    function __window_event_constructor(){

        if(document.all){

            return window.event;

        }

        var _caller = __window_event_constructor.caller;

        while(_caller!=null){

            var _argument = _caller.arguments[0];

            if(_argument){

                var _temp = _argument.constructor;

                if(_temp.toString().indexOf("Event")!=-1){

                    return _argument;

                }

            }

            _caller = _caller.caller;

        }

        return null;

    }

    if(window.addEventListener){

        __firefox();

    }

    /*end firefox------------------------------------------------*/

</script>

 

2.       event.returnValue = false无法使用

虽然重新封装了event但是这个方法还是无法支持

解决方法:

调用event.returnValue = false时候使用以下方法:

function setReturnValueFalse()

{  

    if(document.all)

    {

        window.event.returnValue = false;

    }

    else

    {

        event.preventDefault();

    }

}

 

 

3.       某些click()事件无法触发

有些时候我们会通过js直接调用对象的click()事件,但是有的时候在火狐中却不起作用。

解决方法:

首先查看调用的控件有没有定义onclick事件,火狐中如果没有定义onclick事件的话,调用click()是什么都不会做的,比如:

 

<a href=”http://www.5173.com”>5173</a>

 

A标签在IE下面直接调用该控件click就会跳转到对应的页面,但是火狐下不会,必须添加一个跳转的方法:

 

 

<a href=”http://www.5173.com” onclick=”javascript:if(this.href.length!=0) window.open(this.href)”>5173</a>

 

 

如果在确定有onclick事件的时候仍然调用click()失效,请使用以下方法

function controlClick(obj)

{

     if(document.all)

     {

         obj.click();

     }

     else

     {

         var evt = document.createEvent("MouseEvents");

         evt.initEvent("click", true, true);

         obj.dispatchEvent(evt);

     }

}

 

 

Obj为需要调用click()方法的对象。

4.       清空File控件的内容时候,方法失效。

如果使用document.execCommand(Delete>这个方法清空File控件内容的话,在火狐下是没办法适用的。

解决方法:

调用以下方法

function emptyFile(cid)

{

    var $=document.getElementById;

    document.getElementById(cid).outerHTML?$(cid).outerHTML=$(cid).outerHTML:document.getElementById(cid).value="";

 

}

 

 

其中cid为控件id

 

 

 

5.       调用innerText无效

在火狐中是不支持innerText对象的,请使用innerHTML或者jquerytext()方法

 

 

6.  使用onkeypress事件时候参数使用event.keyCode无效

解决方法:

使用onkeydown方法代替onkeypress或者使用火狐下单独使用event.charCode