我正在开发一个自定义文本编辑器,使用具有基本格式化功能的 contenteditable div。我使用命令
document.execCommand("superscript", false, undefined);
来执行上标功能,例如 EXAMPLETEST。我可以使用什么命令将其恢复为正常文本。
诗。再次点击初始命令不起作用
我已经尝试过
document.execCommand("superscript", false, undefined);
和document.execCommand("removeFormat", false, undefined);
都无法将其关闭。
上标是一个切换功能,仅当文本不存在时才起作用
因此,为了使文本恢复正常,我们必须再次使用该命令,但要确保文本存在,因此有一种方法可以使用
document.queryCommandState()
方法 检查文本是否存在于选择中
if (document.queryCommandState("superscript")) {
document.execCommand("superscript", false, undefined);
} else {
// there is no text
}
使用removeFormat不是最好的选择,因为它会删除所有格式
这有帮助吗?