草庐IT

javascript - webkit stylewithcss contenteditable 不工作?

coder 2025-02-05 原文

我正在尝试使用 contenteditable 和 styleWithCss。

在webkit中好像不行。

每当我使用 execCommand 时,它都会生成一个 <b>而不是我预期的跨度。

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

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

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

非常感谢。

最佳答案

我无法让它与此处两个答案中的命令一起使用。对于那些仍在为这个问题苦思冥想的人,这是如何让它发挥作用的。

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

document.execCommand( command, uiElement, value ) 

正如 Christopher 的优秀答案所详述的那样,“styleWithCSS”的值默认设置为 false 试试看:

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

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

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

这适用于 Firefox 和 Chrome

关于javascript - webkit stylewithcss contenteditable 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3088993/

有关javascript - webkit stylewithcss contenteditable 不工作?的更多相关文章

随机推荐