带有designMode的文本编辑器。如何强制designMode使用HTML标记而不是跨度

时间:2012-06-27 18:24:47

标签: javascript iframe editor designmode

我使用iframe和designMode构建了一个小型文本编辑器。 (这是一个不由我http://jsfiddle.net/Kxmaf/6/编码的示例。我在Stackoverflow上找到了它。

如果我检索iframe(document.body.innerHTML)的内容,结果如下:<span style="font-weight: bold;">Test</span> Text <span style="font-style: italic;">Text</span> <span style="text-decoration: underline;">TT</span>

是否可以将designMode设置为使用标签而不是带有样式属性的跨度?

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

在大多数浏览器(但不是IE)中,您可以使用“StyleWithCSS”命令在样式模式之间切换,允许您使用<b><i>等元素进行样式选择或使用样式设置具有样式属性的<span>个元素。您可以使用“StyleWithCSS”命令执行此操作,在旧版浏览器中返回“UseCSS”。以下开关命令使用非CSS版本:

try {
    if (!document.execCommand("StyleWithCSS", false, useCss)) {
        // The value required by UseCSS is the inverse of what you'd expect
        document.execCommand("UseCSS", false, !useCss);
    }
} catch (ex) {
    // IE doesn't recognise these commands and throws.
}