document.body.clientHeight和document.documentElement.clientHeight區別
本章節介紹一下document.body.clientHeight和document.documentElement.clientHeight的區別。
這裡都已html頂部帶有<!doctype html>考慮,因為現在誰寫頁面還會不寫這個命令呢。
看一段程式碼例項:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> * { margin:0px; padding:0px; } #antzone { width:200px; height:100px; background:#ccc; margin:50px auto; position:absolute; } </style> <script type="text/javascript"> window.onload = function () { var bodyH = document.body.clientHeight; var documentElementhH = document.documentElement.clientHeight; var str = "bodyH:" + bodyH + "<br/>"; str = str + "documentElementhH:" + documentElementhH; antzone.innerHTML = str; } </script> </head> <body> <div id="antzone"></div> </body> </html>
上面的程式碼中,body是沒有高度的,document.body.clientHeight返回值是0,document.documentElement.clientHeight返回的是頁面可見區域的高度,再來看一段程式碼例項:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> * { margin:0px; padding:0px; } body { height:1500px; } #antzone { width:200px; height:100px; background:#ccc; margin:50px auto; position:absolute; } </style> <script type="text/javascript"> window.onload = function () { var bodyH = document.body.clientHeight; var documentElementhH = document.documentElement.clientHeight; var str = "bodyH:" + bodyH + "<br/>"; str = str + "documentElementhH:" + documentElementhH; antzone.innerHTML = str; } </script> </head> <body> <div id="antzone"></div> </body> </html>
設定body的高度,document.body.clientHeight返回值就是這個高度,document.documentElement.clientHeight返回值依然是可見區域的高度。
相關文章
- 和區別
- ../和./和/的區別
- LinkedList和ArrayList的區別、Vector和ArrayList的區別
- http和https的區別/get和post的區別HTTP
- ||和??的區別
- /*和/**的區別
- Synchronize和ReentrantLock區別ReentrantLock
- SSL和TLS 區別TLS
- jquery $(this) 和this的區別jQuery
- ClassNotFoundException和NoClassDefFoundError區別ExceptionError
- substr()和substring()區別
- JQuery this和$(this)的區別jQuery
- insert和insertSelective區別
- SCSS 和 SASS 區別CSS
- T和?的區別
- localStorage和sessionStorage區別Session
- BeanFactory和FactoryBean區別Bean
- Swift和Kotlin區別SwiftKotlin
- makefile =和:=的區別
- undefined 和 null 區別?UndefinedNull
- ibtis # 和 ¥ 區別
- Python之“==”和“is”區別Python
- null和undefined區別NullUndefined
- ++a和a++的區別
- stringbuilder和stringbuffer區別UI
- SpringCloud和Dubbo區別SpringGCCloud
- sleep()和wait()區別AI
- JoinPoint和ProceedingJoinPoint區別
- 127.0.0.0和localhost區別localhost
- ./ 和sh 的區別
- get和post區別
- url和uri區別
- innerHTML 和 innerTEXT 區別HTML
- js基本型別和引用型別區別JS型別
- workman 和swoole 區別 和異同
- Cookie 和 Session 關係和區別CookieSession
- 堆和棧的概念和區別
- springmvc和springboot的區別SpringMVCSpring Boot
- SDK和API的區別?API