Javascript D3键盘上事件返回超链接
我试图在我的网站上做一种“按键导航”:如果按下左箭头,我返回上一页,如果按下右箭头,我进入下一页。我已经这样做了,“console.log(“keydown”)”可以工作,但函数返回的结果却不行Javascript D3键盘上事件返回超链接,javascript,d3.js,keyboard,Javascript,D3.js,Keyboard,我试图在我的网站上做一种“按键导航”:如果按下左箭头,我返回上一页,如果按下右箭头,我进入下一页。我已经这样做了,“console.log(“keydown”)”可以工作,但函数返回的结果却不行 d3.select("body") .on("keydown", function(e) { console.log("keydown"); //return "line_chart.html"; if(e == 37) { // left
d3.select("body")
.on("keydown", function(e) {
console.log("keydown");
//return "line_chart.html";
if(e == 37) { // left
console.log("left");
return "line_chart1.html";
}
else if(e == 39) { // right
console.log("right");
return "line_chart2.html";
}
});
而不是
.on("keydown", function(e) { //e is coming UNDEFINED
console.log("keydown");
//return "line_chart.html";
if(e == 37) { // left i found e as undefined
我使用d3.event.keycode获得了keycode,其工作原理如下:
d3.select("body")
.on("keydown", function(e) {
console.log("keydown");
//return "line_chart.html";
if(d3.event.keycode == 37) { // left
console.log("left");
return "line_chart1.html";//this return will not do anything
}
else if(d3.event.keycode == 39) { // right
console.log("right");
return "line_chart2.html";//this return will not do anything
}
});
编辑
d3.select("body")
.on("keydown", function(e) {
console.log("keydown");
//return "line_chart.html";
if(d3.event.keyCode == 37) { // left
console.log("left");
SOME_FUNCTION("line_chart1.html");
}
else if(d3.event.keyCode == 39) { // right
console.log("right");
SOME_FUNCTION("line_chart2.html");
}
});
或者可以像这样做,
$('body')
,如果OP不想偶然使用d3.event,那么其余的代码保持不变。是的,我同意如果OP使用jquery,那么$('body')是一个选项。我想这可能是另一个答案。我不知道为什么,但我仍然只有向下键输出,而没有“右”或“左”输出,而且链接的返回也不起作用。keyCode
应该是驼峰式的,使用window.location=“line_chart1.html”代码>而不是返回语句,因为您需要页面重定向。谢谢@Gilsha keyCode是一个打字错误:)