我使用 execCommand('bold') 使以下文本变为粗体,但是当我再次尝试禁用粗体时,然后添加其他标签,如“h”或其他标签,它会添加额外的标签链接:
<h1><span style="font-weight: normal;">111</span></h1>
我想知道如何避免这种情况?
最佳答案
它应该有效
首先,如果你真的做得正确的话,就不会发生这样的事情。
请参阅下面的示例,它应该可以工作。
document.designMode = "on";
function myFunction(event) {
if (event.keyCode == 16) {
// Press shift btn exec cmd for bold trigger
document.execCommand("bold");
//check in alert box when triggered
//alert( document.getElementById("thebody").innerHTML );
//check the updated code in console once triggered
console.log( document.getElementById("thebody").innerHTML );
}
}
<!DOCTYPE html>
<html>
<body onkeydown="myFunction(event)">
<div id="thebody">
<h1>Exec execCommand("Bold")</h1>
<p>Try to exec by pressing shift btn once highlighed</p>
<h2>Again execCommand("Bold")</h2>
<p>Select some text in this page, and press the SHIFT button to make the selected text toggle between bold and normal.</p>
</div>
<div id="preview">
</div>
</body>
</html>
注意/建议:
但是,如果您的代码版本中问题仍然存在,请与我分享触发此 exec
Cmd
的函数。
关于javascript - RichEditor 使用 execCommand ('bold' ) 两次附加额外的 <span> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59477616/