Firefox event.clientX无法正常工作

时间:2013-05-24 15:02:55

标签: javascript firefox javascript-events d3.js

我正在研究d3和js项目。

该函数的开头如下:

$(document).ready(function() { 
    d3.select("#aid").select(".abutton").on("mousemove",function() {
        afile.style("top", (event.clientY+10)+"px").style("left",(event.clientX+15)+"px");
        afile.html("<h3>Click text here</p><p>or here</p>");
    }); 

我做了很多谷歌搜索!

本质是在mouseover上,它应该起作用。这适用于Chrome和IE,因为事件变量是全局的,因此它也是客户端*属性。

根据我的理解,解决方案是传入一个eventObject。当我这样做时,我的代码看起来像:

$(document).ready(function() { 
    d3.select("#aid").select(".abutton").on("mousemove",function(event) {
        afile.style("top", (event.clientY+10)+"px").style("left",(event.clientX+15)+"px");
        afile.html("<h3>Click text here</p><p>or here</p>");
    });

FF日志给了我:

[09:59:04.308] TypeError: event is undefined @ filepathofjavascriptfile

同样地,它在Chrome中中断: 未捕获的TypeError:无法读取未定义的filepathofjavascriptfile的属性“clientY” (匿名函数)help.js:34 û

我做错了什么?在此先感谢,如果您还有其他需要,请告诉我。

1 个答案:

答案 0 :(得分:6)

尝试:

d3.select("#aid").select(".abutton").on("mousemove",function() {
    afile.style("top", (d3.event.clientY+10)+"px").style("left",(d3.event.clientX+15)+"px");
    afile.html("<h3>Click text here</p><p>or here</p>");
});

无论出于何种原因,这就是d3如何公开事件对象。

相关问题