2012-12-07 92 views
2

我正在做一個畫廊,並試圖找出如何編程箭頭鍵導航畫廊。下一個點擊鍵按

因此,按下左箭頭會去到以前的圖片網址:

function window.location = 'http://www.example.com/prevphoto.php'; 

按右箭頭會去到下一個圖像的URL。

function window.location = 'http://www.example.com/nextphoto.php'; 

更新(感謝Haxxed指着我在正確的方向) 這是我在哪裏,但它不工作。你能明白爲什麼嗎?

$('body').keypress(function (e) { 
    if (e.which == 37) { 
     // Left Arrow Pushed 
     window.location = "/prevphoto.php"; 
    } else if (e.which == 39) { 
     // Right Arrow Pushed 
     window.location = "/nextphoto.php"; 
    } 
}); 
+0

向我們展示一些代碼或發佈jsFiddle。 – glortho

+0

謝謝大家的意見。他們都幫助。 –

回答

2

基本上你需要使身體標記有一個按鍵事件(或使用jQuery做一個事件偵聽器),然後只檢查鍵碼(左箭頭是37,右爲39),然後使用window.location重定向頁面。 All Key Codes

<head> 
    <script> 
      function e(event){ 
      if(event.keyCode == 37){ 
       //Left Arrow Pushed 
       window.location = "../prevphoto.php"; 
      }else if(event.keyCode == 39){ 
       //Right Arrow Pushed  
       window.location = "../nextphoto.php"; 
      }   
     } 
    </script> 
</head>  
<body onKeyPress="e(event)" > 

</body>​ 
1

嘗試把腳本在你的腦袋標籤,然後修改body使用document

$(document).keypress(function (e) { 

    if (e.which == 37) { 
     //Left Arrow Pushed 
     window.location = "/prevphoto.php"; 
    } else if (e.which == 39){ 
     //Right Arrow Pushed  
     window.location = "/nextphoto.php"; 
    } 

}); 
+0

是的,這似乎是合乎邏輯的,但它也不起作用。這裏的網站[鏈接](http://www.zacharysheldon.com/print/17/Sunset-Light-at-Night) –

+0

嗯,它適用於我。用箭頭鍵沒問題... –