0

我正在开发一个 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,因为事件变量是全局的,因此它的 client* 属性也是如此。

据我了解,解决方案是传入一个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>");
    });

Firefox 日志给了我:

[09:59:04.308] TypeError:事件未定义@filepathofjavascriptfile

同样,它在 Chrome 中中断:

未捕获的类型错误:无法读取未定义文件路径的javascriptfile(匿名函数)help.js:34 的属性“clientY”

我究竟做错了什么?如果您需要其他任何东西,请告诉我。

4

1 回答 1

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 公开事件对象的方式。

于 2013-05-24T15:09:57.817 回答