gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:31:30 27 4
gpt4 key购买 nike

我正在尝试制作一个简单的所见即所得编辑器,但在将文本加粗时遇到问题。我可以使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com