简体   繁体   English

使用jquery增加按钮单击事件的整个网页的字体大小

[英]Increase font size of whole web page on button click event using jquery

I want to increase font size in whole page when user click on increase button. 当用户点击增加按钮时,我想增加整个页面的字体大小。 It will be increase on bases of current font size. 它会在当前字体大小的基础上增加。 Following is sample code : 以下是示例代码:

<div class='parent' style='font-size:15px'>
    <div class='c1'>div 1</div>
    <div class='c2'>div 2</div>
    <div class='c3'>div 3</div>
    <div class='c4'>div 4</div>
    <table style="font-size:17px">
        <tr>
            <th>test 1</th>
            <th style='font-size:11px'><div>test 1.1</div><div style='font-size:22px'>test 1.2</div></th>
            <th>test 2</th>
        </tr>
    </table>
</div>

Here is my jquery code but it will not work properly: 这是我的jquery代码,但它无法正常工作:

$(document).ready(function(){
    $('.parent').find('*').each(function(){
        curSize=$(this).css('font-size');
        arrCurSize=curSize.toUpperCase().split("PX");
        if(arrCurSize.length==2){
            //alert(arrCurSize[0]);
            $(this).css('font-size',((parseInt(arrCurSize[0])+2)+"PX"));
            //alert($(this).css('font-size'));

        }
    });
})

It can be done in one shot. 它可以一次完成。

Just define the inner elements' font-size referencing its container font-size. 只需定义内部元素的font-size引用其容器字体大小。

Eg: 例如:

<div class="container">
   <p class="par1">text1: 15px at start</p>
   <p class="par2">text2: 17px at start</p>
</div>
--------------------------------------------
.container {font-size: 10px}
.par1 {font-size: 1.5em}
.par2 {font-size: 1.7em}

The markup below, gives 15px to first paragraph and 17px to second. 下面的标记给第一段15px,第二段17px。 Because if the standard size setted in the container is 10px, then the conversion rates are: 因为如果容器中设置的标准大小是10px,那么转换率是:

  • 1em = 10px // container initialization 1em = 10px //容器初始化
  • 1.5em = 15px 1.5em = 15px
  • 1.7em = 17px 1.7em = 17px
  • ... and so on ... ... 等等 ...

Now, you can update all font-size definitions, updating the container font-size. 现在,您可以更新所有字体大小定义,更新容器字体大小。

Try this jsFiddle to play with this idea. 尝试这个jsFiddle来玩这个想法。

In addition to my previous comment, here is what I've made so far (again, assuming that you can't modify the HTML part) : http://jsfiddle.net/wared/JWUt9/ . 除了我以前的评论,这是我到目前为止所做的(再次,假设你不能修改HTML部分): http//jsfiddle.net/wared/JWUt9/ This solution is a bit dirty since it adds a style attribute and binds an extra font-size data to every element matched by the selector (in this case : p * ). 这个解决方案有点脏,因为它添加了一个样式属性,并将额外的font-size数据绑定到选择器匹配的每个元素(在本例中为: p * )。 Furthermore, we have to loop through the entire set each time the user clicks the resize button. 此外,每次用户单击调整大小按钮时,我们都必须遍历整个集合。 However, it allows to override fixed sizes defined from non inline styles. 但是,它允许覆盖从内联样式定义的固定大小。

If we had control over non inline styles, we could replace all fixed sizes from there by hand with em units for example, and we could do the same with inline fixed sizes via javascript. 如果我们可以控制非内联样式,我们可以用em单位手动替换所有固定大小,例如,我们可以通过javascript使用内联固定大小。 In the end, we could zoom in and out by simply modifying the container's font-size property, and descendant elements should resize automatically. 最后,我们可以通过简单地修改容器的font-size属性来放大和缩小,并且后代元素应该自动调整大小。 Here is an interesting example from which we could start (scroll down a little to the first codepen demo) : http://css-tricks.com/theres-more-to-the-css-rem-unit-than-font-sizing/ . 这是一个有趣的例子,我们可以从中开始(向下滚动一点到第一个codepen演示): http ://css-tricks.com/theres-more-to-the-css-rem-unit-than-font- 尺寸/

You can simply change font size of the body of whole page in a button click as below. 您只需按一下按钮即可更改整页正文的字体大小,如下所示。 here i changed the font-size in percentage. 在这里我改变了字体大小的百分比。

$("#sizeUp").click(function() {

        $("body").css("font-size","70%");

 });

If you want to change each element size with some amount in each button click, then follow below coding.. 如果您想在每个按钮点击中更改每个元素大小,请按照以下编码..

  $('body *').each(function() {
      xsize= parseInt($(this).css('font-size')) + 1;    
        $(this).css('font-size', xsize+2);
  }); 

试试这个吧

$(this).css("font-size","+=2");

Your question was quite unclear (changing whole page font size) while your coding seems to target the specific container classed .parent 'childrens'. 您的问题很不清楚(更改整页字体大小),而您的编码似乎针对特定的容器分类.parent'childrens'。

[credits to wared and Zaheer Ahmed ] [ 对战Zaheer Ahmed的信誉]

Your coding was close, but the simpliest jQuery method was to .test() with the .attr('style') 您的编码很接近,但最简单的jQuery方法是.test().attr('style')

if (/font-size:[^;]+px/.test($(this).attr('style'))) {
        $(this).css("font-size","+=2");
    }

Here is your solution jsFidled here : 这是你的解决方案jsFidled here

$("#trigger").click(function() {
    /* the overall body tag for relative units */
    $("body").css("font-size","+=5%");
    /* targetting inlined font-size in pixels */     
    $('*').each(function (index, value) {
        if (/font-size:[^;]+px/.test($(this).attr('style'))) {
            $(this).css("font-size","+=2");
        }
    });
});

It's probably simplest to consider formatting your page for this functionality. 考虑为此功能设置页面格式可能是最简单的。 Eg use a fixed original body font-size, and have all elements use a font-size percentage. 例如,使用固定的原始正文字体大小,并使所有元素使用字体大小百分比。 Then, you can simply use jQuery to set the body font-size, and all elements will adjust accordingly. 然后,您可以简单地使用jQuery来设置正文字体大小,并且所有元素都会相应地进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅使用 JavaScript 单击按钮即可增加字体大小 - Increase the font size with a click of a button using only JavaScript 如何使用按钮增加或减少Web浏览器控件的字体大小? - How to increase or decrease web browser control font size using button? 单击按钮无法增加字体大小 - Unable to increase font size on click of a button 当我使用jQuery增大或减小字体大小时,为什么整个页面都会刷新? - Why does the whole page refresh when I use jQuery to increase or decrease the font size? 如何使用Windows Phone中的按钮增加或减少Web浏览器的字体大小? - How to increase or decrease web browser font size using button in windows phone? 如何通过单击按钮来增加或减少页面的字体大小 - How can I increase or decrease font size of page by clicking on a button (使用jQuery)实现增加和减小页面字体大小的按钮的最佳方法是什么? - What's the best way (using jQuery) to implement buttons that increase and decrease the font-size of the page? 如何使用jquery增加字体大小? - How can I increase the font size using jquery? 使用jquery使用行高增加减少字体大小 - increase descrease font size with line height using jquery 字体大小增加和对比度/亮度,使用jquery对网站进行声音控制 - Font size increase and contrast/Brightness,sound control of website using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM