我可以使用向右和向左箭頭鍵將元素的焦點從右向左移動,反之亦然。使用向下箭頭鍵在jQuery中選擇其他元素底部的元素
但是,我也想知道如何選擇上方或下方的元素。我知道jQuery具有next()
和prev()
函數,但是如何關注上面和下面的函數?
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="./index.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<h1 >First</h1><h2>Second</h2>
<button class="move">something</button>
<button class="move">Third</button>
<h1 >Another</h1>
<h3 >Five</h3>
<button class="move">another button</button>
<script type="text/javascript" src="./index.js"></script>
</body>
</html>
的.js
$(".move:first").focus();
$(document).keydown(
function(e)
{
if (e.keyCode == 39) {
$(".move:focus").next().focus();
}
if (e.keyCode == 37) {
$(".move:focus").prev().focus();
}
if (e.keyCode == 38) {
}
if (e.keyCode == 40) {
}
}
);
所以,你要4個按鈕,同比增長>向下>左>右你想根據客戶按鍵的方向來設置焦點? – NewToJS
「上方」和「下方」將根據視口進行更改,您是否考慮過這一點? – progrAmmar