webkit stylewithcss contenteditable不工作?

时间:2010-06-21 22:39:09

标签: javascript jquery webkit wysiwyg contenteditable

我正在尝试使用contenteditable和styleWithCss。

它似乎不适用于webkit。

我在任何时候使用execCommand,它会生成<b>而不是我期望的范围。

这是一个演示: http://jsbin.com/izomo/2/edit

选择文本的一部分,单击粗体按钮,然后查看html输出。

这是一个错误还是我做错了什么。

非常感谢。

3 个答案:

答案 0 :(得分:10)

我无法在这里的答案中使用这些命令。对于那些仍然对这个问题感到头疼的人来说,这就是如何让它发挥作用。

我们可以将三个值传递给execCommand

document.execCommand( command, uiElement, value ) 

正如Christopher的优秀答案详细说明,“styleWithCSS”的值默认设置为false 试试吧:

alert(document.queryCommandState("styleWithCss"));

要将其设置为true,您需要将第三个参数“value”传递为true。像这样:

document.execCommand("styleWithCSS", null, true);

这适用于Firefox和Chrome

答案 1 :(得分:3)

似乎一切都按预期工作。请参阅WebKit BugZilla上的Bug 13490

编辑: 2009年2月3日,changeset 40560在WebKit的来源中添加了对styleWithCSS的支持。

也就是说,从那时起,无论如何,styleWithCSS始终设置为false,而在更改之前,样式命令始终使用CSS,就好像{{1存在,但始终设置为styleWithCSS

我甚至尝试按照Mozilla documentation重写您的true行:

document.execCommand

这些修改后的命令仍可在Firefox中使用,但不适用于Chrome 5或Safari 5(Windows上和今天安装)。

所以,看起来这是一个WebKit错误。

答案 2 :(得分:1)

document.execCommand("styleWithCSS", true, null);
document.execCommand("bold", false, null);
相关问题