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