javascript - document.execCommand 在 Firefox 和 IE 中不起作用

标签 javascript jquery html internet-explorer firefox

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

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

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

完整代码如下:

<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>

有什么想法吗?

最佳答案

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

关于javascript - document.execCommand 在 Firefox 和 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20792662/

相关文章:

javascript - "parentheses after function call "是 JavaScript 的正确/合法使用吗?

javascript - jQuery 动态内容的点击不起作用

javascript - 如果在 uib-tabset 中使用,则值不与范围的变量绑定(bind)

javascript - 是否可以禁用 Canvas 元素上的上下文菜单?

javascript - 调整 div 的大小以保持比例

jquery - 获取 jquery.each() 函数的变量 OUT

javascript - 将鼠标悬停在列表上时显示文本

javascript - 链接管道操作后如何从 Angular 服务中返回可观察值

javascript - 在 anchor 链接到另一个页面并显示 div

html - 使 swf 成为带有指针图标的可点击链接