简体   繁体   English

Firefox 上的 event.target

[英]event.target on Firefox

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

Works fine on Google Chrome and IE but not on Firefox.在 Google Chrome 和 IE 上工作正常,但在 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.谷歌说event.srcElement (适用于 IE 但不适用于 Firefox)所以我添加了event.target但仍然无法正常工作。 Is there anymore changes I need to do to work on Firefox?要在 Firefox 上工作,我还需要做其他更改吗? By the way I'm using 3.5 version of Firefox.顺便说一下,我使用的是 3.5 版的 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" ;
       } 

Please help me to make it work on Firefox请帮助我使其在 Firefox 上运行

Make sure you define event as a formal parameter to the handler.确保将event定义为处理程序的形式参数。

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. IE是全局定义的, Chrome两个地方都定义了,所以两种方式都可以,但Firefox只将其定义为函数参数。

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" ;
} 

I solved my problem using Jquery.我使用 Jquery 解决了我的问题。 For example to get the id of a element you can use:例如,要获取元素的 id,您可以使用:

var x = $(this).attr('id');

This solution to work in Firefox browser too.此解决方案也适用于 Firefox 浏览器。 When start keydown action then its set the event property to global variable like this.当开始keydown操作时,它将event属性设置为这样的全局变量。

 var keyEvent=null; 

Assign that event property to keyEvent and .item is target element.将该event属性分配给keyEvent并且.item是目标元素。

  $(document).on("keydown",'.item', function(e){ 

                keyEvent=e;                                    

        },this))

then.Use keyEvent in your code.然后。在您的代码中使用keyEvent

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

Note : Don't try to set the event property to global variable of window .注意:不要尝试将event属性设置为window全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM