Recently I found that many friends asked: How to change the font size of an article using js?
The editor has reviewed relevant articles and compiled several small cases for your reference. The specific contents are as follows
Rendering:
Click the big and small buttons to switch the font size at any time
Specific code:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>使用js如果改变一篇文章字体的大小</title> </head> <style type="text/css"> a{ text-decoration:none; color:#0C3} a:hover{ color:#F36} </style> <body> <script> function changesize(size) { document.getElementById("article_content").style.fontSize =size+"px"; } </script> <div id="article_content">脚本之家<br/>脚本之家欢迎您<p>好好学习 天天向上</div> <a href="javascript:changesize('20')">大</a> <a href="javascript:changesize('12')">小</a> </body> </html>
Let me share another chestnut with you:
The working principle is very simple. It is to change the font size of the article when the event is triggered. To be more straightforward, it is to change the value of the font-size attribute (jQuery version 1.7.2)
HTML
<div class="box"> <div class="ctrl"> <a href="javascript:;">放大</a> <a href="javascript:;">缩小</a> <a href="javascript:;">默认</a> </div> <div class="cont">这里是一些文字</div> </div>
CSS
.box{text-align:center;} .ctrl{padding:50px 0px 0px 0px;background:#f4f4f4;font-size:0px;border-bottom:3px solid #333;} .ctrl a{display:inline-block;width:50px;height:30px;line-height:30px;background:#333;color:#fff;font-size:14px;} .ctrl a:hover{background:#444;color:#fff;font-weight:700;text-decoration:none;} .cont{padding-top:50px;font-size:14px;}
JS
$(function(){ function sizeIn(){ var sizeCont = parseInt($(".cont").css("fontSize")); // 获取原设定的font-size的值 if(sizeCont == 30){ // 判断font-size增大到30像素时停止 $(".cont").css({fontSize:sizeCont}); }else{ $(".cont").css({fontSize:sizeCont + 1}); } } function sizeOut(){ var sizeCont = parseInt($(".cont").css("fontSize")); if(sizeCont == 10){ // 判断font-size减小到10像素时停止 $(".cont").css({fontSize:sizeCont}); }else{ $(".cont").css({fontSize:sizeCont - 1}); } } function sizeDefault(){ $(".cont").css({fontSize:""}) } $(".ctrl a").click(function(){ if($(this).index() == 0){ sizeIn(); }else if($(this).index() == 1){ sizeOut(); }else{ sizeDefault(); } }) });
I hope this article will be helpful to everyone learning JavaScript programming.