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