jQuery height()
此方法返回或設定匹配元素的高度。
語法結構一:
[JavaScript] 純文字檢視 複製程式碼.height()
沒有引數的時候是獲取第一個匹配元素height屬性值。
jQuery1.0版本新增。
語法結構二:
[JavaScript] 純文字檢視 複製程式碼.height(value)
引數解析:
value:可以是一個整數(單位預設是px),或者一個帶單位的整數(字串)。
將所有匹配元素的height設定為value。
jQuery1.0版本新增。
語法結構三:
[JavaScript] 純文字檢視 複製程式碼.height(function(index, oldHeight))
引數解析:
function:返回匹配元素新高度的回撥函式。index:可選,當前元素在匹配元素集合中的索引(從0開始),oldHeight:可選,當前匹配元素的height值。在函式中,this指向當前元素。
也就是height函式的引數是function函式的返回值。
jQuery1.4.1版本新增。
程式碼例項:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> p{ width:200px; height:200px; background-color:green; } </style> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { alert($("p").height()) }) }) </script> </head> <body> <p>螞蟻部落</p> <input type="button" id="bt" value="檢視演示"/> </body> </html>
獲取p元素的height屬性值。
特別說明:如果匹配元素又多個,只會獲取第一個匹配元素的height屬性值。
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> p{ width:200px; height:200px; background-color:green; } </style> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { $("p").height(400); }) }) </script> </head> <body> <p></p> <input type="button" id="bt" value="檢視演示"/> </body> </html>
設定所有匹配元素的height屬性值。
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> p{ width:200px; height:200px; background-color:green; } </style> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { $("p").height(function(index,h){ return h + 50; }) }) }) </script> </head> <body> <p></p> <input type="button" id="bt" value="檢視演示"/> </body> </html>
h是元素原本的高度,上面的程式碼也就是給元素高度再增加50px。
相關文章
- jQuery之height()、innerHeight()、outerjQuery
- screen.height
- height:100%,height:100vh什麼區別呢
- css的height:100%和height:inherit有什麼區別?CSS
- 讓height: 100%生效
- jQuery無法獲取隱藏元素(display:none)寬度(width)和高度(height)的新解決方案jQueryNone
- canvas ImageData height 屬性Canvas
- canvas ImageData.height 屬性Canvas
- CSS line-height 行高CSS
- css17 CSS Height, WidthCSS
- height百分比失效
- height:100%失效解決辦法
- CSS深入理解之line-heightCSS
- CSS:line-height繼承時的坑CSS繼承
- jQuery初探:自制jQueryjQuery
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- 我的’jQuery’和jQueryjQuery
- 關於line-height和元素高度的那些事
- vue中設定height:100%無效的問題Vue
- 深入理解-CSS內聯元素之line-heightCSS
- CSS 如何讓auto height完美支援過渡動畫?CSS動畫
- jQueryjQuery
- jQuery入門(三)--- jQuery語法jQuery
- jquery列印頁面(jquery.jqprint)jQuery
- JQuery模板外掛-jquery.tmpljQuery
- 一個由line-height引發的血案與思考
- 【前端Talkking】CSS系列——CSS深入理解之line-height前端CSS
- 處理Android安卓line-height無法垂直居中Android安卓
- HTML 中塊級元素設定 height:100% 的實現HTML
- JQuery基本知識彙總;JQuery常用方法;淺入瞭解JQueryjQuery
- 區別margin、padding、width、height值為百分比padding
- vue3 設定el-dialog height超過捲軸Vue
- 如何解決html設定height:100%無效的問題?HTML
- 為智慧世界“高”歌:HEIGHT,五種風景,一個答案
- jquery的onjQuery
- 分解jQueryjQuery
- jQuery(三)jQuery
- jQuery - AJAXjQuery
- jQuery htmljQueryHTML