【发布时间】:2013-05-15 20:46:24
【问题描述】:
我正在创建一个 HTML 富文本编辑器,我想在 div 上使用 contenteditable 属性。我在一个名为编辑器的 div 中有一个 div 列表。当用户按回车键时,我会阻止默认操作。然后我在当前 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);
})
});
【问题讨论】:
-
另外,在您的示例中,所有 div 的内部编辑器都有
contenteditable="ture".. 这应该说 TRUE :)
标签: javascript contenteditable