Firefox 上的 event.target [英] event.target on Firefox

查看:36
本文介绍了Firefox 上的 event.target的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 var x = event.target||event.srcElement;
 document.getElementById(x.id).style.left =  200 + "px" ;
 document.getElementById(x.id).style.top  =  100 + "px" ;

在 Google Chrome 和 IE 上运行良好,但在 Firefox 上运行不正常.在谷歌上试过.谷歌说 event.srcElement(适用于 IE 但不适用于 Firefox)所以我添加了 event.target 但仍然无法工作.在 Firefox 上工作我需要做更多的改变吗?顺便说一下,我使用的是 3.5 版的 Firefox.

Works fine on Google Chrome and IE but not on Firefox. Tried it on Google. Google says event.srcElement (works on IE but not on Firefox) so I have added event.target but still not working. Is there anymore changes I need to do to work on Firefox? By the way I'm using 3.5 version of Firefox.

   function up()
       {
            dragok = false;
            document.onmousemove = null;
            var x = event.target||event.srcElement;
            document.getElementById(x.id).style.left= 200 + "px" ;
            document.getElementById(x.id).style.top= 100 + "px" ;
       } 

请帮助我让它在 Firefox 上运行

Please help me to make it work on Firefox

推荐答案

确保将 event 定义为处理程序的形式参数.

Make sure you define event as a formal parameter to the handler.

IE 是全局定义的,而 Chrome 在两个地方都定义了,所以两种方式都可以,但是 Firefox 只定义为一个函数参数.

IE defines it globally, and Chrome defines it both in both places, so it works either way, but Firefox only defines it as a function parameter.

function up( e ) {
    //       ^-----------------------------------------------------+
    if( !e ) e = window.event; // <---needed this --- and this ->--+

    dragok = false;
    document.onmousemove = null;
    var x = e.target||e.srcElement; // <--- and these
    document.getElementById(x.id).style.left= 200 + "px" ;
    document.getElementById(x.id).style.top= 100 + "px" ;
} 

这篇关于Firefox 上的 event.target的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆