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

查看:63
本文介绍了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上工作不正常.在Google上尝试过.Google说 event.srcElement (可在IE上运行,但不能在Firefox上运行),因此我添加了 event.target ,但仍无法正常工作.在Firefox上需要进行其他更改吗?顺便说一下,我使用的是Firefox的3.5版本.

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天全站免登陆