jQuery控制网页字体大小

<div class="msg">
    <div class="msg_caption">
        <button type="button" class="bigger">放大</button>
        <button type="button" class="smaller">缩小</button>
    </div>
    <div>
        <p class="para" >
            “On an evening in the latter part of May a middle-aged man 
            was walking homeward from Shaston to the village of Marlott, 
            in the adjoining Vale of Blakemore, or Blackmoor. 
            The pair of legs that carried him were rickety, 
            and there was a bias in his gait which inclined him 
            somewhat to the left of a straight line. 
            He occasionally gave a smart nod, 
            as if in confirmation of some opinion, 
            though he was not thinking of anything in particular. 
            An empty egg-basket was slung upon his arm, 
            the nap of his hat was ruffled, 
            a patch being quite worn away at its brim 
            where his thumb came in taking it off. 
            Presently he was met by an elderly parson astride on a gray mare, 
            who, as he rode, hummed a wandering tune.”
        </p>
    </div>
</div>
<script>
    $(function(){
        $("button").click(function(){
            var fontSize = $(".para").css("font-size");  //获取字体大小
            var textFontSize = parseInt(fontSize);  //去掉单位
            var unit = fontSize.slice(-2);  //获取单位
            var btn = $(this).attr("class"); //获取button的属性值
            if(btn == "bigger"){
                if(textFontSize <= 30){  //判断最大字体
                    textFontSize += 2;
                }
            } else if(btn == "smaller"){
                if(textFontSize >= 14){  //判断最小字体
                    textFontSize -= 2;
                }
            }
            $(".para").css("font-size",textFontSize + unit);
        });
    })
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值