D3键盘事件返回超链接

时间:2016-06-22 09:42:19

标签: javascript d3.js keyboard

我试图做一种"键导航"在我的网站上:如果按了left_arrow,我会返回上一页,如果按下right_arrow,我会转到下一页。我已经完成了这个," console.log(" keydown")"有效,但功能返回不是。

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";
      }
  });

1 个答案:

答案 0 :(得分:1)

而不是

.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获得了密钥代码,它的工作原理如下:

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");
          }
        });