Javascript 如何使用箭头键将文本区域从一个div聚焦到另一个div

Javascript 如何使用箭头键将文本区域从一个div聚焦到另一个div,javascript,jquery,html,Javascript,Jquery,Html,这是我的div结构,我想使用上下箭头键将光标从一个div移动到另一个div。有什么解决办法吗 <div id="InsertDiv"> <div draggable="true" onclick="openPropDyn(event)" data-serial="6018" ondragstart="drag(event,6018)" class="subtaskDetailDive complete" id="subtaskDetailDive6018">

这是我的div结构,我想使用上下箭头键将光标从一个div移动到另一个div。有什么解决办法吗

<div id="InsertDiv">
    <div draggable="true" onclick="openPropDyn(event)" data-serial="6018" ondragstart="drag(event,6018)" class="subtaskDetailDive complete" id="subtaskDetailDive6018">
        <div class="subtaskRow" id="readOnlyID6018">
            <textarea></textarea>
        </div>
    </div>
    <div draggable="true" onclick="openPropDyn(event)" data-serial="6019" ondragstart="drag(event,6019)" class="subtaskDetailDive complete" id="subtaskDetailDive6019">
        <div class="subtaskRow" id="readOnlyID6019">
            <textarea></textarea>
        </div>
    </div>
    <div draggable="true" onclick="openPropDyn(event)" data-serial="6020" ondragstart="drag(event,6020)" class="subtaskDetailDive complete" id="subtaskDetailDive6020">
        <div class="subtaskRow" id="readOnlyID6020">
            <textarea></textarea>
        </div>
    </div>
    <div draggable="true" onclick="openPropDyn(event)" data-serial="6021" ondragstart="drag(event,6021)" class="subtaskDetailDive complete" id="subtaskDetailDive6021">
        <div class="subtaskRow" id="readOnlyID6021">
            <textarea></textarea>
        </div>
    </div>
    <div draggable="true" onclick="openPropDyn(event)" data-serial="6022" ondragstart="drag(event,6022)" class="subtaskDetailDive complete" id="subtaskDetailDive6022">
        <div class="subtaskRow" id="readOnlyID6022">
            <textarea></textarea>
        </div>
    </div>
</div>

$(文档).keydown(
职能(e){
如果(e.keyCode==40){
$(“.move:focus”).closest(“.subtaskDetailDive”).next().find(“.move”).focus();
}
如果(e.keyCode==38){
$(“.move:focus”).closest('.subtaskDetailDive').prev().find('.move').focus();
}
}
);

$(文档).keydown(
职能(e){
如果(e.keyCode==40){
$(“.move:focus”).closest(“.subtaskDetailDive”).next().find(“.move”).focus();
}
如果(e.keyCode==38){
$(“.move:focus”).closest('.subtaskDetailDive').prev().find('.move').focus();
}
}
);

要做到这一点,您必须编写jQuery代码

$('.subtaskRow textarea').on('keyup', function(e) {
    if(e.keyCode == 38){ // Key code for Up key
      $(".subtaskRow textarea:focus").closest('.subtaskDetailDive').prev().find('.subtaskRow textarea').focus();
      console.log("Up key Pressed");
    }      
    else if(e.keyCode == 40){ // Key code for Down key
      $(".subtaskRow textarea:focus").closest('.subtaskDetailDive').next().find('.subtaskRow textarea').focus();
      console.log("Down key Pressed");
    }
});
有关输出,请参阅此js FIDLE链接
要做到这一点,您必须编写jQuery代码

$('.subtaskRow textarea').on('keyup', function(e) {
    if(e.keyCode == 38){ // Key code for Up key
      $(".subtaskRow textarea:focus").closest('.subtaskDetailDive').prev().find('.subtaskRow textarea').focus();
      console.log("Up key Pressed");
    }      
    else if(e.keyCode == 40){ // Key code for Down key
      $(".subtaskRow textarea:focus").closest('.subtaskDetailDive').next().find('.subtaskRow textarea').focus();
      console.log("Down key Pressed");
    }
});
有关输出,请参阅此js FIDLE链接

请注意,如果在文本区域处于焦点时覆盖箭头键,则无法使用相同的键在element.yes@Taplar中导航插入符号。有一个contenteditable div。我想使用箭头键从一个移动到另一个。谢谢。这个检查可能重复。我需要从一个div移动到另一个div。不输入,我可以使用事件处理输入导航。感谢@slackerNote,如果您在文本区域处于焦点时覆盖箭头键,您将无法使用相同的键在element.yes@Taplar中导航插入符号。有一个contenteditable div。我想使用箭头键从一个移动到另一个。谢谢。这个检查可能重复。我需要从一个div移动到另一个div。不输入,我可以使用事件处理输入导航。谢谢@slacker