当前位置:首页 > JavaScript 笔记 > JS中event事件在兼容IE、火狐(Firefox)处理办法

JS中event事件在兼容IE、火狐(Firefox)处理办法

snuday15年前 (2011-05-25)JavaScript 笔记1190

前期用到了一个键盘左右键控制翻页的js,在ie里面直接就可以使用,可在火狐(firefox)里面就是不好使,想了半天怎么也不可能不好使呀,所以找了多方资料才知道,原来火狐对于js中event事件不解析,必须要转换到函数里面在调用出来,唉,本来挺简单的两句代码,现在要多加个函数,但是考虑到火狐,也只好忍了~~~

<script>
/*firefox*/
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>

以上为兼容火狐Firefox的js部分,下面是真正的翻页部分:

<SCRIPT>
<!--
document.onkeyup=function()
{
if(event.keyCode==37)window.location.href=document.getElementById("up").href;
if(event.keyCode==39)window.location.href=document.getElementById("down").href;
}

-->
</SCRIPT>


相关文章

IE6-7-8 JS关闭窗口不提示的方法(强制关闭)

 IE6-7 JS关闭窗口不提示的方法 2推荐方法一:js 代码function CloseWin() //这个不会提示是否关闭浏览器{window.opener=null;//w...

50条网页制作小技巧集锦

控制横向和纵向滚动条的显隐?<body style="overflow-y:hidden"> 去掉x轴<body style="overflow-x:h...

window.open 居中显示__ 绝对好用,不妨试试~~

<script type="text/javascript">function openwindow(url,name,iWidth,iHeight){ &n...

同页面多个JS冲突一般性解决方法+EcShop JS一般性总结(2)

2、养成良好的js代码编写习惯①. 总是使用 ‘var’在javascript中,变量不是全局范围的就是函数范围的,使用”var”关键词将是保持变量简洁...

兼容IE6、IE7、IE8、firefox等浏览器全屏浮动广告代码(已测试有效)

   前几天让做一个广告浮动,本来很容易的,不巧的是网站上好多JS给相互打架了,而且火狐还从中作梗,费了点力气才找到了一个比较有效的代码------- 不多说了下面是演示...

a href=#与X-UA-Compatible标签完整解读

<a href="javascript:void(0)"  onClick="<%=onclick%>"...