我有一个contenteditable元素,每当我输入一些内容并点击ENTER它时,都会创建一个新元素并将<div>新的行文本放置在其中。我一点都不喜欢。是否可以防止这种情况发生,或者至少将其替换为<br>?这是演示http://jsfiddle.net/jDvau/
3 回答

慕哥6287543
TA贡献1831条经验 获得超10个赞
尝试这个
$('div[contenteditable]').keydown(function(e) {
// trap the return key being pressed
if (e.keyCode === 13) {
// insert 2 br tags (if only one br tag is inserted the cursor won't go to the next line)
document.execCommand('insertHTML', false, '<br><br>');
// prevent the default behaviour of return key pressed
return false;
}
});
添加回答
举报
0/150
提交
取消