首先,设置两个按钮,点击按钮可增加或减小字号!
<span style="margin-left:30px;">字体:<a onclick="add()" style="font-size:18px;font-weight:bold;" href="javascript:SetFont()">A+</a>
<a onclick="min()" style="font-size:16px;font-weight:bold;" href="javascript:SetFont()">A-</a>
</span>
然后就是写js代码,
var isClick=false;
var size=16;
var maxsize=22;
var minsize=14;
function add(){
isClick = true;
if(size>=maxsize){alert("不能再大了!!!")}
else{
size+=1;
}
}
function min(){
isClick = true;
if(size<=minsize){alert("不能再小了!!!")}
else{size-=1;}
}
function SetFont() {
var FontArea = document.getElementById("nr");//给内容范围设置一个id nr
if (isClick==true){
console.log("当前字体大小"+size+"px");
}
else{
size=size;
}
if(!FontArea)
{
return;
}
FontArea.style = 'font-size:'+size+ 'px';
var tagname=["td","span","div","a","li","ul","ol","p","font","b","tr","strong","table","section","pre","article","h1","h2","h3","h4","h5","h6"];
var length=tagname.length;
//遍历数组中的元素,逐一赋予字号
for(var k=0;k<length;k++){
tag=FontArea.getElementsByTagName(tagname[k]);
for(var i=0;i<tag.length;i++){
tag[i].style.fontSize=size+'px';
}
}
}