关于execCommand的错误

时间:2015-05-20 11:56:47

标签: javascript

<select onchange="setFontSize(this)">
    <option value="20">20</option>
    <option value="30">30</option>
    <option value="14">14</option>
</select>
<div id="pannel" contenteditable="true">
像这样的HTML。

function setFontSize(obj){
        document.execCommand("fontSize",false,obj.value)
}

是这样的。

我有一些问题。

  1. fontsize并不像我想的那样工作(我的意思是选择的值对于字体大小没有影响)
  2. document.getElementsByTagName('select')[0].onchange=setFontSize(this)不会&#39;工作(从html单独的js)

2 个答案:

答案 0 :(得分:0)

您只能使用1-7范围内的字体。 Look here

function setFontSize(obj){
        document.execCommand("fontSize",false,obj.value)
}
<html>
<head>
</head>

<body>
 
<select onchange="setFontSize(this)">
    <option value="1">1</option>
    <option value="3" selected>3</option>
    <option value="7">7</option>
</select>
<div id="pannel" contenteditable="true">
Select this text and change select option.
</body>
</html>

答案 1 :(得分:0)

参考https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand

字体大小 更改所选内容或插入点的字体大小。这需要一个 1-7 的整数作为值参数。

你可以使用

styleWithCSS