event.target not working on Firefox

前端 未结 3 852
情书的邮戳
情书的邮戳 2020-11-27 07:13
 var x = event.target||event.srcElement;
 document.getElementById(x.id).style.left =  200 + \"px\" ;
 document.getElementById(x.id).style.top  =  100 + \"px\" ;


        
相关标签:
3条回答
  • 2020-11-27 07:40

    This solution to work in Firefox browser too. When start keydown action then its set the event property to global variable like this.

     var keyEvent=null; 
    

    Assign that event property to keyEvent and .item is target element.

      $(document).on("keydown",'.item', function(e){ 
    
                    keyEvent=e;                                    
    
            },this))
    

    then.Use keyEvent in your code.

         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.

    0 讨论(0)
  • 2020-11-27 07:54

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

    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" ;
    } 
    
    0 讨论(0)
  • 2020-11-27 07:59

    I solved my problem using Jquery. For example to get the id of a element you can use:

    var x = $(this).attr('id');
    
    0 讨论(0)
提交回复
热议问题