DIV CSS 佈局教程網

用 jQuery 設定指定元素的寬與高
編輯:JQuery特效代碼     

本文示範如何使用 jQuery 的 width() 與 height() 方法,動態設定指定元素的寬度與高度。

<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").width(500).height(500);
  });
});
</script>
</head>
<body>
<div id="div1" style="height:100px;width:300px;padding:10px;margin:3px;
border:1px solid blue;background-color:lightblue;"></div>
<br>
<button>Resize div</button>
</body>
</html>

width() 與 height() 既能讀取、也能設定元素尺寸;由於支援串接(chaining),可以像 .width(500).height(500) 這樣一次設定兩個值。希望本文對大家的 jQuery 開發有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved