Javascript Firefox点击事件错误
我真的不知道如何为你们重现这个问题,但我会尽力向你们解释。我有一个d3树布局结构。单击父气泡应展开其子气泡,依此类推。在我的代码中,我目前有:Javascript Firefox点击事件错误,javascript,jquery,firefox,d3.js,Javascript,Jquery,Firefox,D3.js,我真的不知道如何为你们重现这个问题,但我会尽力向你们解释。我有一个d3树布局结构。单击父气泡应展开其子气泡,依此类推。在我的代码中,我目前有: // Enter any new nodes at the parent's previous position. var nodeEnter = node.enter().append("g") .attr("class", "node") .attr("transform", function(d) { return "transla
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("mousedown", function(d, event) {
if(event.ctrlKey){
if (d.shortName != undefined) {
modalOpen(d);
}
else{
click(d);
}
}
//event.which is handled in chrome
//event.button is handled in IE10
else if((event.which == 1)||(event.button == 1)||(event == 1)){
click(d);
}
});
FireFox无法识别event.ctrlKey,但需要识别。有一个问题。主要问题是else-if语句。我有event.which用于chrome,event.button用于IE,而FireFox中的event似乎只给了我一个整数。Mozilla开发者网络页面上描述的mouseevent.button属性表示:
The button number that was pressed when the mouse event was fired:
Left button=0, middle button=1 (if present), right button=2.
For mice configured for left handed use in which the button actions are
reversed the values are instead read from right to left.
我总是尝试左键单击,但有时会得到0或2的值。如果我尝试右键单击,将得到1、0或2。它从来就不是真正一致的。如果我进入最子节点并定期单击,我会得到一个int值,19、9、3、2、0或1。在这种情况下,19和9似乎是最一致的。我不知道为什么Firefox会如此困难,但我希望你们中的一位能帮助我解决这个问题,这样就不会每次都这样 在d3中设置事件处理程序时,传递的两个参数不是数据和事件(如您假设的),而是数据和索引,如下所示:
.on("mousedown", function(d, i) {
// 'd' is the datum
// 'i' is the index
})
.on("mousedown", function(d, i) {
if(d3.event.ctrlKey){
// your code here
}
})
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("mousedown", function(d, i) {
if(d3.event.ctrlKey){
if (d.shortName != undefined) {
modalOpen(d);
} else {
click(d);
}
}
//event.which is handled in chrome
//event.button is handled in IE10
else if((d3.event.which == 1)||(d3.event.button == 1)||(d3.event == 1)){
click(d);
}
});
API文档()对此进行了描述:
选择.on(类型[,侦听器[,捕获]])
将事件侦听器添加或删除到当前
选择,用于指定的类型。该类型是字符串事件类型
名称,如“单击”、“鼠标悬停”或“提交”。(任何DOM事件类型)
可使用浏览器支持的。)侦听器由
使用命名约定装饰选定的DOM元素
“_uontype”。指定的侦听器的调用方式与
其他操作员功能,传递当前数据d和索引
i、 使用此上下文作为当前DOM元素。访问
侦听器中的当前事件,使用全局d3.event。返回
忽略事件侦听器的值
这就解释了为什么你会得到随机整数——索引会根据你点击的元素而变化。要获取鼠标事件,请使用全局d3.event,如下所示:
.on("mousedown", function(d, i) {
// 'd' is the datum
// 'i' is the index
})
.on("mousedown", function(d, i) {
if(d3.event.ctrlKey){
// your code here
}
})
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("mousedown", function(d, i) {
if(d3.event.ctrlKey){
if (d.shortName != undefined) {
modalOpen(d);
} else {
click(d);
}
}
//event.which is handled in chrome
//event.button is handled in IE10
else if((d3.event.which == 1)||(d3.event.button == 1)||(d3.event == 1)){
click(d);
}
});
因此,您的最终代码应该如下所示:
.on("mousedown", function(d, i) {
// 'd' is the datum
// 'i' is the index
})
.on("mousedown", function(d, i) {
if(d3.event.ctrlKey){
// your code here
}
})
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("mousedown", function(d, i) {
if(d3.event.ctrlKey){
if (d.shortName != undefined) {
modalOpen(d);
} else {
click(d);
}
}
//event.which is handled in chrome
//event.button is handled in IE10
else if((d3.event.which == 1)||(d3.event.button == 1)||(d3.event == 1)){
click(d);
}
});
您确定不调用复数MouseEvent.buttons()属性吗?这可以解释为什么整数一直到19。我怎么告诉@vtosh?实际上,我刚刚删除了我的代码中查找左键的部分,并且正在努力正确地获取事件。不用说,它仍然不起作用。我已经看过并尝试过这里描述的方法,但它不起作用。除此之外,firefox中的程序没有遵循我的if语句的流程。它跳入另一个if中嵌套的else。真是一团糟!