1
我也跟着下面的教程這是應該允許鍵盤滾動並通過無序列表項選擇 - >Tutorial與預期的JQuery不是演戲鍵盤導航
我用它旁邊的意見箱描述 - >Suggestion Box
請注意,除了更改ID和名稱之外,我對這些信息都有跟隨。
它似乎工作(當我按下懸停效果確實顯示),但它會立即消失後,我放開了鑰匙。如果我按住向下箭頭,它將滾動到我的列表底部,但一旦釋放按鍵,懸停效果立即消失。 JQuery絕不是我強大的套件,所以我不確定我做錯了什麼。我完全按照教程中的描述使用代碼。我只是將#menu更改爲#suggestions,因爲這是我的div的名稱。
這裏然而代碼:
var currentSelection = 0;
var currentUrl = '';
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function()
{
// Register keypress events on the whole document
$(document).keypress(function(e) {
switch(e.keyCode) {
// User pressed "up" arrow
case 38:
navigate('up');
break;
// User pressed "down" arrow
case 40:
navigate('down');
break;
// User pressed "enter"
case 13:
if(currentUrl != '') {
window.location = currentUrl;
}
break;
}
});
// Add data to let the hover know which index they have
for(var i = 0; i < $("#suggestions ul li a").size(); i++) {
$("#suggestions ul li a").eq(i).data("number", i);
}
// Simulote the "hover" effect with the mouse
$("#suggestions ul li a").hover(
function() {
currentSelection = $(this).data("number");
setSelected(currentSelection);
}, function() {
$("#suggestions ul li a").removeClass("itemhover");
currentUrl = '';
}
);
});
function navigate(direction) {
// Check if any of the menu items is selected
if($("#suggestions ul li .itemhover").size() == 0) {
currentSelection = -1;
}
if(direction == 'up' && currentSelection != -1) {
if(currentSelection != 0) {
currentSelection--;
}
} else if (direction == 'down') {
if(currentSelection != $("#suggestions ul li").size() -1) {
currentSelection++;
}
}
setSelected(currentSelection);
}
function setSelected(menuitem) {
$("#suggestions ul li a").removeClass("itemhover");
$("#suggestions ul li a").eq(menuitem).addClass("itemhover");
currentUrl = $("#suggestions ul li a").eq(menuitem).attr("href");
}
任何人可以或許協助我在這件事情嗎?
嘗試在'而不是keydown' .. – elclanrs
難道這點。沒有解決這個問題。完全一樣的東西。 – Herm