我基于execCommand
和queryCommandState
(现在为obsolete)创建了一个内容丰富的编辑器。我正在寻找这些命令的替代品,尤其是
document.queryCommandState('bold')
我认为以下是一个好的开始:
window.getSelection().getRangeAt(0);
我得到了当前选择,但是我无法弄清楚该选择是否为粗体<b>
。
答案 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);
}
}
}