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

