Code _javascript tips for controlling font size through JavaScript

Source: Internet
Author: User
For this code to go to your Web page you must use the pixel size font (PX) instead of the relative size of the font, using "EM" or "%". Of course, if you use other font units of code can easily adapt to these. If the script cannot find the font size of a paragraph, it defaults to 12px.
Core code:
Copy Code code as follows:

var min=8;
var max=18;
function Increasefontsize () {
var p = document.getelementsbytagname (' P ');
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 (' P ');
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"
}
}

Include the above code in your Web page (either placed in the header section, or place it in an external JS file and import). You can then invoke the ability to increase and decrease the font size like the following.
How to use:
Copy Code code as follows:

<a href= "javascript:decreasefontsize ();" >-</a>
<a href= "javascript:increasefontsize ();" >+</a>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.