HTML リッチ テキスト エディターを作成しており、div で contenteditable 属性を使用したいと考えています。エディターと呼ばれる div 内に div のリストがあります。ユーザーがEnterキーを押すと、デフォルトのアクションが防止されます。次に、現在の div の後に新しい div を挿入します。唯一の問題は、新しい div に集中できないことです。誰もこれを行う方法を知っていますか?
HTML
<body>
<div id="editor" contenteditable="true">
<div id="1" contenteditable="ture" class="line" style="height: 20px; background-color: #a0f;"></div>
<div contenteditable="ture" class="line" style="height: 20px; background-color: #abf;"></div>
<div contenteditable="ture" class="line" style="height: 20px; background-color: #a9f;"></div>
<div contenteditable="ture" class="line" style="height: 20px; background-color: #aff;"></div>
<div contenteditable="ture" class="line" style="height: 20px; background-color: #aaf;"></div>
</div>
</body>
JavaScript
$(function(){
var thisLine;
$("#editor").bind("keydown", function(event){
if(event.keyCode == 13){
event.preventDefault();
var newLine = $("<div tabindex=\"-1\" contenteditable=\"ture\" id=\"2\"class=\"line\" style=\"height: 20px; background-color: #aff;\"></div>");
newLine.insertAfter(thisLine);
$("#2").focus();
}
})
$(".line").bind("click", function(){
thisLine = $(this);
})
});