javascript - 为什么 execCommand ('bold' ) 什么都不做?

标签 javascript jquery html wysiwyg

我正在尝试制作一个简单的所见即所得编辑器,但在将文本加粗时遇到问题。我可以使用 document.execCommand("underline", false, null); 为文本添加下划线并将文本设为斜体 document.execCommand("italic", false, null);但是document.execCommand("bold", false, null);什么都不做。

我检查了 html 输出,它没有添加任何 <b><strong>为文本添加标签。

这是 HTML:

<button id="underline" type="button">Underline</button>
<button id="italic" type="button">Italic</button>
<button id="bold" type="button">Bold</button>
<div id="editor" contenteditable="true" spellcheck="false"></div>

这是 jQuery:

$('#underline').click(function() {
   document.execCommand("underline", false, null);
});

$('#italic').click(function() {
   document.execCommand("italic", false, null);
});

$('#bold').click(function() {
   document.execCommand("bold", false, null);
});

最佳答案

据我所知,这是有效的。看看这个 fiddle :http://jsfiddle.net/om78patL/

$('#underline').click(function() {
   document.execCommand("underline", false, null);
});

$('#italic').click(function() {
   document.execCommand("italic", false, null);
});

$('#bold').click(function() {
   document.execCommand("bold", false, null);
});

<button id="underline" type="button">Underline</button>
<button id="italic" type="button">Italic</button>
<button id="bold" type="button">Bold</button>
<div id="editor" contenteditable="true" spellcheck="false">test</div>

关于javascript - 为什么 execCommand ('bold' ) 什么都不做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53843240/

相关文章:

javascript - 使用 Angular 选择未按预期工作

javascript - 在 Handlebars 中的另一个辅助函数中使用一个辅助函数的结果

javascript - 在javascript中动态获取php数组

html - 无法在 Bootstrap 3x 上隐藏子菜单

javascript - 在html网站中移动圆圈

javascript - 如何选择前第三个 DOM 元素的子元素?

javascript - 如何使用 Phonegap 和 javascript 在 iPhone 上隐藏和显示键盘

javascript - 浏览器页面重新加载后计时器刷新 - Javascript

java - 将ajax GET请求参数转换为域对象并传递给Spring Controller,如@RequestBody

javascript - 在 JQGrid 的一列中添加超链接,然后单击“超链接”应调用 Jquery 函数