How Add Default Text Size Function - Javascript

2019-09-14 00:05发布

问题:

I found the following script for increase and decrease font size but does anyone have any idea how to do the function for default font?

var min=8;
var max=18;
function increaseFontSize() {
   var p = document.getElementsByTagName('div');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('div');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}

回答1:

function resetToDefaultFontSize() {
   var p = document.getElementsByTagName('div');
   for(i=0;i<p.length;i++) {
      p[i].style.fontSize = "12px";
   }
}

Note: jQuery could make all these functions into 3 one-liners, so you might want to look into it.

www.jquery.com