替代/替代queryCommandState('bold')

时间:2020-03-23 09:00:51

标签: javascript html contenteditable

我基于execCommandqueryCommandState(现在为obsolete)创建了一个内容丰富的编辑器。我正在寻找这些命令的替代品,尤其是

document.queryCommandState('bold')

我认为以下是一个好的开始:

window.getSelection().getRangeAt(0);

我得到了当前选择,但是我无法弄清楚该选择是否为粗体<b>

2 个答案:

答案 0 :(得分:5)

可以通过window.getSelection()

完成

JSFiddle: https://jsfiddle.net/Imabot/s54zoxk2/

说明: https://lucidar.me/en/rich-content-editor/lightweight-rich-content-editor-part-2-check-if-bold/

希望这对其他人有帮助...

答案 1 :(得分:1)

您可以获取节点的所有计算样式,然后检查属性的值。

function getComputedStyles(currentNode) {
    if(currentNode.id != maxTreeNodeId) {
        try {
            var styles = window.getComputedStyle(currentNode);
            console.log(styles.fontWeight); // Print font weight, 700 = bold
        } catch(err) {
            this.getComputedStyles(currentNode.parentNode);
        }
    }
}