JQ 控制字体大小

style.css

* { margin:0; padding:0; }
.msg {width:300px; margin:100px; }
.msg_caption { width:100%; overflow:hidden; margin-bottom:1px;}
.msg_caption span { display:block; float:left; margin:0 2px; padding:4px 10px; background:#898989; cursor:pointer;font-size:12px;color:white; }
.msg textarea{ width:300px; height:80px;height:100px;border:1px solid #000;}

demo.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script src="js/jquery-1.10.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
	$("span").click(function(){
		var thisEle = $("#para").css("font-size"); 
		var textFontSize = parseFloat(thisEle , 10);
		var unit = thisEle.slice(-2); //获取单位
		var cName = $(this).attr("class");
		if(cName == "bigger"){
			   if( textFontSize <= 22 ){
					textFontSize += 2;
				}
		}else if(cName == "smaller"){
			   if( textFontSize >= 12  ){
					textFontSize -= 2;
				}
		}
		$("#para").css("font-size",  textFontSize + unit);
	});
});
</script>
</head>
<body>
<div class="msg">
	<div class="msg_caption">
		<span class="bigger" >放大</span>
		<span class="smaller" >缩小</span>
	</div>
	<div>
		<p id="para">
        阅谁问君诵,水落清香浮。阅谁问君诵,水落清香浮。阅谁问君诵,水落清香浮。
        阅谁问君诵,水落清香浮。阅谁问君诵,水落清香浮。阅谁问君诵,水落清香浮。
        阅谁问君诵,水落清香浮。阅谁问君诵,水落清香浮。阅谁问君诵,水落清香浮。
		</p>
	</div>
</div>
</body>
</html>

效果图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2288921
jq