【问题标题】:webkit stylewithcss contenteditable not working?webkit stylewithcss contenteditable 不起作用?
【发布时间】:2010-06-21 22:39:09
【问题描述】:

我正在尝试使用 contenteditable 和 styleWithCss。

它似乎在 webkit 中不起作用。

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

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

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

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

非常感谢。

【问题讨论】:

    标签: javascript jquery webkit wysiwyg contenteditable


    【解决方案1】:

    我无法让它与此处两个答案中的命令一起使用。对于那些还在为这个问题头疼的人来说,这是如何让它发挥作用。

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

    document.execCommand( command, uiElement, value ) 
    

    作为 Christopher 的优秀答案详细信息,“styleWithCSS”的值默认设置为 false 试试看:

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

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

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

    这适用于 Firefox 和 Chrome

    【讨论】:

      【解决方案2】:

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

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

      也就是说,似乎从那时起,无论如何,styleWithCSS 总是设置为 false,而在更改之前,样式命令总是与 CSS 一起应用,好像 styleWithCSS 存在但一直存在设置为true

      我什至尝试按照Mozilla documentation 重写您的document.execCommand 行如下:

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

      这些修改后的命令在 Firefox 中仍然有效,但在 Chrome 5 或 Safari 5 中均无效(在 Windows 上并已安装)。

      所以,这似乎是一个 WebKit 错误。

      【讨论】:

      • 那是stylewithcss false,我正在尝试stylewithcss true。
      • 天啊!好的。我会潜水。
      • 与原版相同的错误,但现在方向相反?经过更多研究后,我更新了答案。我认为添加styleWithCSS 支持应该使其可配置,但它停留在其默认状态,即false。 (见Editor.cpp:940。)
      • 不幸的消息,但谢谢。确认错误后,我会接受您的回答。
      • 第三个参数是你想要的命令值。对于StyleWithCSS,您需要一个布尔型第三个参数。第二个参数几乎总是无关紧要的,可以是false。因此document.execCommand("styleWithCSS", false, true);
      【解决方案3】:
      document.execCommand("styleWithCSS", true, null);
      document.execCommand("bold", false, null);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-04
        相关资源
        最近更新 更多