ReferenceError: event is not defined in Firefox

后端 未结 2 1405
闹比i
闹比i 2020-12-21 10:25

I am using this function:

$(\"#tooltip\").animate({ left: event.clientX, top: event.clientY });

Its working on Chrome and IE but in Firefox

相关标签:
2条回答
  • 2020-12-21 10:35

    You appear to be trying to use the odd global event object that is a legacy of the 4.0-browser-era Microsoft event model. Don't. Use the standard event object instead. It will be the first argument to your event handler function.

    jQuery('selector').on('click', function (evt) {
        jQuery("#tooltip").animate({ left: evt.clientX, top: evt.clientY });
    });
    
    0 讨论(0)
  • 2020-12-21 10:40

    Mothana, Quentin is right,almost. I work with d3.js (jQuery like library) and the old story browser thing is that events that are not declared are not acceptable. So you have 3 weays to solve this:

    1. Don't use events - not our case i think.

    2. Use the standard event object of your function - Example in Quentin's answer.

    3. Make your javascript library object do the work for you. Example in d3.js:
    I had this not working code:

        tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px"))
    

    and i used the d3.js object named "d3" to make the selection of the event for me (working code):

    tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px")
    

    So I suppose in jQuery you must use the name of your javascript library object, something like jQuery.someSelection().

    0 讨论(0)
提交回复
热议问题