更改 contentEditable div 的字体大小

问题描述 投票:0回答:2
javascript html css contenteditable
2个回答
6
投票

document.execCommand("fontSize"...
命令仅接受“HTML 字体大小”(1-7)。 请参阅API

fontSize - 更改选择或插入时的字体大小 观点。这需要将 HTML 字体大小 (1-7) 作为 价值论证

您尝试使用较大的尺寸作为参数(12...36)来执行命令,以便该命令接近您可能想要的最接近的尺寸。因为所有参数都大于 7,所以近似值始终为 7。

如果确切的像素高度对您来说并不重要,那么最好的解决方案是更改代码以使用不精确的 html 字体大小。

另请参阅: document.execCommand() 字体大小(以像素为单位)?


0
投票

嘿我给你找到答案了

HTML:

 <div class="font-size">
                        <p>font-size:</p>
                        <button id="increase" onclick="increase()">A+</button>
                        <button id="decrease" onclick="decrease()">a-</button>
                    </div>

JS:

 function increase() {
      txt = document.getElementById('Your text id');
      style = window.getComputedStyle(txt, null).getPropertyValue('font-size');
      selection = window.getSelection();
      currentSize = parseFloat(style);
      txt.style.fontSize = (currentSize + 1) + 'px';
      document.getElementById("Write").innerHTML = text.replace(selection, txt)
    }
    function decrease() {
      txt = document.getElementById('You text id');
      style = window.getComputedStyle(txt, null).getPropertyValue('font-size');
      currentSize = parseFloat(style);
      txt.style.fontSize = (currentSize - 1) + 'px';
    }
© www.soinside.com 2019 - 2024. All rights reserved.