Home > Article > Web Front-end > How to set font size in javascript
How to set the font size in JavaScript: First set the layout and write two buttons; then click the plus and minus buttons to control the size of the text. The code is [oPtxt.style.fontSize=num 'px'].
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
How to set the font size in javascript:
First of all, look at the layout, write a simple sentence, and write two buttons:
<input id="Btn1" type="button" value="+" name=""/> <input id="Btn2" type="button" value="-" name=""/> <p id="p1" style="font-size:14px;">这是一些文字这是一些文字这是一些文字这是一些文字</p>
Effect The picture is as follows:
#Then, you need to control the text size by clicking the plus and minus buttons. The js code is as follows:
<script> window.onload= function(){ var oPtxt=document.getElementById("p1"); var oBtn1=document.getElementById("Btn1"); var oBtn2=document.getElementById("Btn2"); var num = 14; /*定义一个初始变量*/ oBtn1.onclick = function(){ num++; oPtxt.style.fontSize=num+'px'; }; oBtn2.onclick = function(){ num--; oPtxt.style.fontSize=num+'px'; } } </script>
Related free Learning recommendation: javascript video tutorial
The above is the detailed content of How to set font size in javascript. For more information, please follow other related articles on the PHP Chinese website!