【问题标题】:document.execCommand not working in Firefox and IEdocument.execCommand 在 Firefox 和 IE 中不起作用
【发布时间】:2014-01-14 13:43:04
【问题描述】:

我正在尝试为我的文本区域设置一些粗体、斜体和下划线按钮,我正在使用此代码从正常切换到粗体/斜体/下划线。

document.execCommand("bold", false, "");

它在 Chrome 中完美运行,但在 Firefox 中,即它不会将文本更改为粗体或任何内容,我不知道为什么?

这里是完整的代码:

<input name="new_shout_bold" type="button" class="new_shout_text_option" id="new_shout_bold" onclick='document.execCommand("bold", false, "");' value="B" alt="B" />

<input name="new_shout_italic" type="button" class="new_shout_text_option" onclick='document.execCommand("italic", false, "");' value="I" alt="I" style="font-style: italic;"/>

<input name="new_shout_underline" type="button" class="new_shout_text_option" onclick='document.execCommand("underline", false, "");' value="U" alt="U" style="text-decoration: underline;" />

这里是文本区域:

<div id="dash_new_shout_textarea" name="dash_new_shout_textarea" class="dash_new_shout_textarea" contenteditable="true"></div>

有什么想法吗?

【问题讨论】:

    标签: javascript jquery html internet-explorer firefox


    【解决方案1】:

    我知道这不会有太大帮助, 但这段代码在 Chrome 和 Firefox 中运行良好

    http://jsbin.com/iluXOGe/2/edit

    【讨论】:

    • 你使用的是什么版本的火狐?我的是 26 版
    【解决方案2】:

    通过在 HTML 标签中添加unselectable="on" 使按钮在 IE 中不可选择。

    【讨论】:

    • 这在 chrome 和 firefox 中绝对完美。一件事,在Firefox中是否可以将光标不断地放在文本上,所以当您单击粗体按钮时,光标不会停止,因此您可以继续输入而不必再次单击?
    • @Arken:是的:调用 contenteditable 元素的focus() 方法。在您的情况下,即使用事件处理程序属性,这意味着将 document.getElementById("dash_new_shout_textarea").focus() 添加到 onclick 属性值的末尾。 jsfiddle.net/Nfx3r/1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2015-08-16
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多