如何在Javascript中更改fontsize

时间:2017-10-31 12:44:10

标签: javascript html font-size

为什么这段代码不起作用;

HTML:

    <div>  
       <button id="jsstyle" onclick="js_style()">Click to change table size</button> 
    </div>

JavaScript的:

function js_style () {
  document.getElementById ("table").text.style.fontSize = "14pt";  
}     

4 个答案:

答案 0 :(得分:1)

你的桌子在哪里?没有text属性,只需像style.fontSize那样:

&#13;
&#13;
<div><button id="jsstyle" onclick="js_style()">Click to change table size</button></div>

<table id="table">
<tr>
<td>asdasdas</td>
</tr>
</table>

<script>
  function js_style() {

    document.getElementById("table").style.fontSize = "14pt";
  }
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以这样做:

document.getElementById("table").style.fontSize = "14pt";

以下是文档:https://www.w3schools.com/jsref/prop_style_fontsize.asp

答案 2 :(得分:0)

你会找到文件here 试试这个:

<!DOCTYPE html>
<html>
<body>

<p id="myP">This is a paragraph.</p>

<button type="button" onclick="myFunction()">Set font size</button>
 
<script>
function myFunction() {
    document.getElementById("myP").style.fontSize = "50pt";
}
</script>

</body>
</html>

答案 3 :(得分:0)

使用jsstyle ID代替table并在text工作后删除id

&#13;
&#13;
function js_style() {

  document.getElementById("jsstyle").style.fontSize = "48pt";

}
&#13;
<head>
  <meta charset=utf-8 />
</head>

<div> <button id="jsstyle" onclick="js_style()">Click to change table size</button> </div>
&#13;
&#13;
&#13;