使用Chrome中的document.execCommand(“insertorderedlist”)丢失样式

问题描述 投票:1回答:1

以下代码段在Chrome上丢失了自定义样式,但在Firefox上则没有:

    document.getElementById( 'run' ).addEventListener( 'click', function() {
      document.execCommand("insertorderedlist");
    } );
  <div contenteditable="true">
    <p><span style="font-weight: bold;">line 1</span></p>
    <p><span style="font-style: italic;">line 2</span></p>
    <p><span style="text-decoration-line: underline;">line 3</span></p>
  </div>
  
  <button id="run">Select the above text and click me</button>

有没有已知的解决方法?我找不到任何关于它的文档。

javascript html google-chrome execcommand
1个回答
0
投票

您需要获得选择共同祖先的样式(在本例中,它是一个文本节点,因此您必须获取parentNode),并将其应用于新选择的共同祖先:

document.getElementById( 'run' ).addEventListener( 'click', function() {
        sel = window.getSelection();
        if (sel.rangeCount) {
            let selection = sel.getRangeAt(0);
            if (selection.startContainer.parentNode.tagName === 'SPAN') {
              let startNode = selection.startContainer.parentNode.parentNode;
              let endNode = selection.endContainer.parentNode.parentNode;
              let styles = [startNode.firstChild.style.cssText];
              while(startNode && !startNode.isEqualNode(endNode)) {
                  startNode = startNode.nextElementSibling || startNode.nextSibling;
                  styles.push(startNode.firstChild.style.cssText);
              }
              document.execCommand("insertorderedlist");
              selection = sel.getRangeAt(0);
              startNode = selection.startContainer.parentNode;
              endNode = selection.endContainer.parentNode;
              newNodes = [startNode];
              while(startNode && !startNode.isEqualNode(endNode)) {
                  startNode = startNode.nextElementSibling || startNode.nextSibling;
                  newNodes.push(startNode);
              }
              for (var i = 0; i < newNodes.length; i++) {
                 newNodes[i].style = styles[i];
              }
            } else {
              if (sel.rangeCount) {
                  let ancestor = sel.getRangeAt(0).commonAncestorContainer.parentNode;
                  document.execCommand("insertorderedlist");
                  sel.getRangeAt(0).commonAncestorContainer.parentNode.style = ancestor.style.cssText;
              }
          }
        }
        
    } );
<div contenteditable="true">
    <p><span style="font-weight: bold;">line 1</span></p>
    <p><span style="font-style: italic;">line 2</span></p>
    <p><span style="text-decoration-line: underline;">line 3</span></p>
  </div>
  
  <button id="run">Select the above text and click me</button>
© www.soinside.com 2019 - 2024. All rights reserved.