【发布时间】:2011-10-21 05:56:25
【问题描述】:
我正在尝试使用 contentEditable div 编写所见即所得的编辑器,但在 Firefox 中处理粗体(和斜体)时遇到了问题。
当 div 中的所有文本都被选中时,execCommand('bold') 在 div 中起作用,但是当我尝试抓取该内容的 HTML 时,HTML 不会显示任何格式。
要明白我的意思,请尝试在 Firefox 5 中运行以下 HTML 代码:
<script type="text/javascript">
window.onload = function () {
var output = document.getElementById('output');
var input = document.getElementById('input');
}
</script>
<body>
<button onClick="execCommand('bold', false, null);output.value=input.innerHTML;">Bold</button>
<div style="width: 300px; border: 1px solid #000000;">
<div id="input" contenteditable="true">Some editable text</div>
</div>
<textarea id="output"></textarea>
</body>
</html>
请尝试以下方法:
- 仅选择“一些”一词。单击粗体按钮。正如预期的那样,这将使文本变为粗体。 HTML 输出使用
<span style="font-weight:bold;">将单词加粗,这有点令人遗憾(在 Safari、Chrome 中是<b>),但仍然可以完成工作。 - 选择整个短语“一些可编辑的文本”(手动或使用 CTRL-A)。单击粗体按钮。虽然 contentEditable 文本是粗体,但正如预期的那样,HTML 输出没有应用粗体格式。
任何关于可能导致这些问题的原因以及如何解决这些问题的想法都将不胜感激!
【问题讨论】:
-
在 Firefox 5 中,这是我得到的 html:
<div contenteditable="true" id="input" style="font-weight: bold;">Some editable text</div>。由于整个input已应用bold,因此它将其作为样式应用到input,而不是创建span。
标签: html select contenteditable bold