jQuery之字體大小的設置方法
先獲取字體大小,進行處理。
再將修改的值保存。
slice() 方法可從已有的數組中返回選定的元素。
arrayObject.slice(start,end)。
start 必需。規(guī)定從何處開始選取。如果是負數,那么它規(guī)定從數組尾部開始算起的位置。也就是說,-1 指最后一個元素,-2 指倒數第二個元素,以此類推。
end 可選。規(guī)定從何處結束選取。該參數是數組片斷結束處的數組下標。如果沒有指定該參數,那么切分的數組包含從 start 到數組結束的所有元素。如果這個參數是負數,那么它規(guī)定的是從數組尾部開始算起的元素。
jQuery代碼如下:
<script type="text/javascript">
$(function(){
$("span").click(function(){
//獲取para的字體大小
var thisEle = $("#para").css("font-size");
//parseFloat的第二個參數表示轉化的進制,10就表示轉為10進制
var textFontSize = parseFloat(thisEle , 10);
//javascript自帶方法
var unit = thisEle.slice(-2); //獲取單位
var cName = $(this).attr("class");
if(cName == "bigger"){
textFontSize += 2;
}else if(cName == "smaller"){
textFontSize -= 2;
}
//設置para的字體大小
$("#para").css("font-size", textFontSize + unit );
});
});
</script>
html代碼如下:
<body>
<div class="msg">
<div class="msg_caption">
<span class="bigger" >放大</span>
<span class="smaller" >縮小</span>
</div>
<div>
<p id="para" >
This is some text. This is some text. This is some text. This is some text. This
is some text. This is some text. This is some text. This is some text. This is some
text. This is some text. This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text. This is some text. This
is some text. This is some text.
</p>
</div>
</div>
</body>
相關文章
淺析Js(Jquery)中,字符串與JSON格式互相轉換的示例(直接運行實例)
這幾天,遇到了json格式在JS和Jquey的環(huán)境中,需要相互轉換,在網上查了一下,大多為缺胳膊少腿,也許咱是菜鳥吧,終于測試成功后,還是給初學者們一個實例吧2013-07-07
jQuery綁定事件不執(zhí)行但alert后可以正常執(zhí)行
這篇文章主要為大家解決下為什么jQuery綁定事件不執(zhí)行而alert后可以正常執(zhí)行,需要的朋友可以參考下2014-06-06

