jQuery中文參考手冊(cè)
/ jQuery 尺寸
jQuery 尺寸
jQuery 尺寸
通過(guò) jQuery,很容易處理元素和瀏覽器窗口的尺寸。
jQuery 尺寸方法
jQuery 提供多個(gè)處理尺寸的重要方法:
width()
height()
innerWidth()
innerHeight()
outerWidth()
outerHeight()
jQuery 尺寸
jQuery width() 和 height() 方法
width() 方法設(shè)置或返回元素的寬度(不包括內(nèi)邊距、邊框或外邊距)。
height() 方法設(shè)置或返回元素的高度(不包括內(nèi)邊距、邊框或外邊距)。
下面的例子返回指定的 <div> 元素的寬度和高度:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ var txt=""; txt+="div 的寬度是: " + $("#div1").width() + "</br>"; txt+="div 的高度是: " + $("#div1").height(); $("#div1").html(txt); }); }); </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>顯示 div 元素的尺寸</button> <p>width() - 返回元素的寬度</p> <p>height() - 返回元素的高度</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
jQuery innerWidth() 和 innerHeight() 方法
innerWidth() 方法返回元素的寬度(包括內(nèi)邊距)。
innerHeight() 方法返回元素的高度(包括內(nèi)邊距)。
下面的例子返回指定的 <div> 元素的 inner-width/height:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ var txt=""; txt+="div 寬度: " + $("#div1").width() + "</br>"; txt+="div 高度: " + $("#div1").height() + "</br>"; txt+="div 寬度,包含內(nèi)邊距: " + $("#div1").innerWidth() + "</br>"; txt+="div 高度,包含內(nèi)邊距: " + $("#div1").innerHeight(); $("#div1").html(txt); }); }); </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>顯示 div 元素的尺寸</button> <p>innerWidth() - 返回元素的寬度 (包含內(nèi)邊距)。</p> <p>innerHeight() - 返回元素的高度 (包含內(nèi)邊距)。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
jQuery outerWidth() 和 outerHeight() 方法
outerWidth() 方法返回元素的寬度(包括內(nèi)邊距和邊框)。
outerHeight() 方法返回元素的高度(包括內(nèi)邊距和邊框)。
下面的例子返回指定的 <div> 元素的 outer-width/height:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ var txt=""; txt+="div 寬度: " + $("#div1").width() + "</br>"; txt+="div 高度: " + $("#div1").height() + "</br>"; txt+="div 寬度,包含內(nèi)邊距和邊框: " + $("#div1").outerWidth() + "</br>"; txt+="div 高度,包含內(nèi)邊距和邊框: " + $("#div1").outerHeight(); $("#div1").html(txt); }); }); </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>顯示 div 元素的尺寸</button> <p>outerWidth() - 返回元素的寬度 (包含內(nèi)邊距和邊框)。</p> <p>outerHeight() - 返回元素的高度 (包含內(nèi)邊距和邊框)。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例