Home > Article > Web Front-end > How to set font size in javascript
How to set the font size in JavaScript: 1. Create the "function setFontSize (id, content, params) {...}" method; 2. Pass "setFontSize (id, contentid, {...}) ” method can be debugged.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
How to set font size in javascript?
JavaScript method of controlling font size setting
When making the company's official website, the news page will have a function that allows viewers to adjust the text size themselves, so during this free time Time, sort out this function:
function setFontSize (id,content,params){ var oTarget = document.getElementById(id), content = document.getElementById(content), size = params.size || 14, maxSize = params.maxSize || 20, step = params.step || 2; oBtn = '<input type="button" value="+"/><input type="button" value="-" />'; oBtn1 = null, oBtn2 = null; oTarget.innerHTML = oBtn; oBtn1 = oTarget.childNodes[0]; oBtn2 = oTarget.childNodes[1]; oBtn1.onclick=function(){ if(size+step <= maxSize){ size+=step; }else{ size = maxSize; this.className+=' disabled'; this.disabled = true; } oBtn2.className.replace('disabled',''); oBtn2.disabled = false; content.style.fontSize = size +'px'; } oBtn2.onclick=function(){ if(size-step >= 12){ size-=step; }else{ size = 12; this.className+=' disabled' this.disabled = true; } oBtn1.className.replace('disabled',''); oBtn1.disabled = false; content.style.fontSize = size +'px'; } }
Calling method:
setFontSize(id,contentid,{size:,maxSize,step:}); /* * id :用于存放增加或减小两个按钮的父级盒子的id。 * contentid: 存放内容的id。 * {} 一个对象,用于提供设置的参数。 |— szie : 字体起始(默认)大小。 |— maxSize : 最大字体。 |— step : 增长的步长值。 */
Tip: You can hide the value of the Input element through font-size:0, and then customize it The style of the button.
[Recommended learning: javascript basic 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!