【问题标题】:how to change fontsize in Javascript [closed]如何在Javascript中更改字体大小[关闭]
【发布时间】:2018-04-12 15:27:10
【问题描述】:

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

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";  
}     

【问题讨论】:

  • 删除text。这样做:document.getElementById("table").style.fontSize = "14pt";
  • 什么是table

标签: javascript html font-size


【解决方案1】:

你会发现文档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>

【讨论】:

  • 最好提一下,尝试什么。
  • @Durga 添加了文档链接:)
【解决方案2】:

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

function js_style() {

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

}
<head>
  <meta charset=utf-8 />
</head>

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

【讨论】:

    【解决方案3】:

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

    <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>

    【讨论】:

      【解决方案4】:

      你可以这样做:

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

      这里是文档:https://www.w3schools.com/jsref/prop_style_fontsize.asp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-09
        • 2015-11-20
        • 2012-03-08
        • 2013-06-20
        • 2011-07-31
        • 2017-10-25
        相关资源
        最近更新 更多