html - 避免 ie contentEditable 元素在 Enter 键上创建段落

标签 html contenteditable

在 InternetExplorer 上,contentEditable DIV 会在您每次按 Enter 时创建一个新段落 (<p></p>),而 Firefox 会创建一个 <br/>标签。 是否可以强制 IE 插入 <br/>而不是新段落?

最佳答案

这是一个解决方案(使用 jQuery)。单击“更改为 BR”按钮后,<br>将插入标记而不是 <p></p>标签。

HTML:

<div id='editable' contentEditable="true">
This is a division that is content editable. You can position the cursor 
within the text, move the cursor with the arrow keys, and use the keyboard 
to enter or delete text at the cursor position.
</div>
<button type="button" onclick='InsertBR()'>Change to BR</button>
<button type="button" onclick='ViewSource()'>View Div source</button>

Javascript:

function InsertBR()
{
    $("#editable").keypress(function(e) {
        if (e.which == 13) 
        {
            e.preventDefault();
            document.selection.createRange().pasteHTML("<br/>")     
        }
    });
}

function ViewSource()
{           
    var div = document.getElementById('editable');
    alert('div.innerHTML = ' + div.innerHTML);
}

These links helped . 工作示例 here .

关于html - 避免 ie contentEditable 元素在 Enter 键上创建段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2057881/

相关文章:

html - 实现 HTML5 等距游戏的最佳方式?

javascript - Vue js如何在prop更改时调用方法

html - 更改 HTML5 数据列表中的输入值

javascript - 使用 insertHTML 的 execCommand 的 iframe 撤消/重做(contenteditable)

editor - 与 ContentEditable 一起使用的好的 WYSIWYG 编辑器是什么?

javascript - polymer/深度/选择器在移动设备中不起作用

html - 悬停时更改整个 div 的背景颜色

jquery - Contenteditable 显示最后插入的 html 元素

javascript - 如何从 contentEditable div 获取 html 以进入隐藏文本表单元素内部?

javascript - contentEditable div 内表格上的 keydown