在 InternetExplorer 上,contentEditable DIV 会在<p></p>
您每次按 Enter 时创建一个新段落 ( ),而 Firefox 会创建一个<br/>
标签。是否可以强制 IE 插入 a<br/>
而不是新段落?
问问题
11004 次
5 回答
8
这是一个解决方案(使用 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);
}
于 2010-01-26T22:56:57.437 回答
7
是的,可以通过先停止 keydown 事件 ( window.event.stopPropagation();
) 然后使用插入HTML 命令插入字符串来避免插入段落。
然而,IE 依赖于这个 div 来设置样式等,使用 <br>s 会遇到麻烦。
我建议您使用TinyMCE 或其他编辑器之类的项目,并搜索行为方式符合您要求的编辑器,因为它们具有针对不同浏览器问题的各种解决方法。也许你可以找到一个使用 <br>s 的编辑器...
于 2010-01-13T15:58:02.650 回答
6
您总是可以学习将SHIFT+ENTER用于单行返回和ENTER段落返回。IE 在这方面表现得像 MS Word。
于 2010-01-29T13:18:15.963 回答
1
如果你可以使用它,FCKEditor对此有一个设置
于 2010-01-26T18:53:16.980 回答
1
更改可编辑作品line-height
的内部:<p>
<div>
#editable_div p
{
line-height: 0px;
}
于 2012-09-05T15:37:03.140 回答