一個JavaScript擷取元素當前高度的執行個體,javascript擷取

來源:互聯網
上載者:User

一個JavaScript擷取元素當前高度的執行個體,javascript擷取

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一個JavaScript執行個體-擷取元素當前高度</title> <style> #date{width:90%;height:25%;padding:10px;background: red;} </style> <script> window.onload = function(){ var height = getHeight(); console.log(height); } function getHeight(){ var height = 0; var div = document.getElementById("date").getBoundingClientRect(); if(div.height){ height = div.height; }else{ height = div.bottom - div.top; } return height; } </script> </head> <body> <div style="width:1000px;height:800px;"> <div id = "date"> </div> </div> </body> </html>


javascript怎擷取頁面的高度與寬度

strInfo+=\r\n網頁可見地區高:+document.body.clientHeight;
strInfo+=\r\n網頁可見地區寬:+document.body.offsetWidth+(包括邊線的寬);
strInfo+=\r\n網頁可見地區高:+document.body.offsetHeight+(包括邊線的高);
strInfo+=\r\n網頁本文全文寬:+document.body.scrollWidth;
strInfo+=\r\n網頁本文全文高:+document.body.scrollHeight;
strInfo+=\r\n網頁被捲去的高:+document.body.scrollTop;
strInfo+=\r\n網頁被捲去的左:+document.body.scrollLeft;
strInfo+=\r\n網頁本文部分上:+window.screenTop;
strInfo+=\r\n網頁本文部分左:+window.screenLeft;
strInfo+=\r\n螢幕解析度的高:+window.screen.height;
strInfo+=\r\n螢幕解析度的寬:+window.screen.width;
strInfo+=\r\n螢幕可用工作區高度:+window.screen.availHeight;
strInfo+=\r\n螢幕可用工作區寬度:+window.screen.availWidth;
window.confirm(strInfo);
</script
-----------------------------------------------------------------------------------------
最近編程中發現html靜態頁面的開發可以使用
document.body.ClientHeight
來獲得頁面的高度,但asp.net的頁面卻無法這樣擷取,擷取到的只是頁面最小的高度,比如頁面只有一個button,那麼該js語句獲得的就是24。
document.documentElement.OffsetHeight
----------------------------------------------------------------------------------------加上document.body.scrollLeft;
document.body.scrollTop;
x=document.body.clientWidth+document.body.scrollLeft;
y=document.body.clientHeight+document.body.scrollTop;若想得到整個頁面的高度可以用
 
javascript中擷取元素的問題

id是具有唯一性的,也就是說同一頁面中不允許有同名的id存在,所以通過id擷取網頁元素時,獲得的是單一的元素,不是數組,所以就不需要加[0]了,如:
getElementById("abc")
而名字(name)或標籤名(TagName)則是允許同名的,所以通過它們擷取的網頁元素都是數組(即使不存在同名現象,只有一個,也是數組),要調用其中的某個元素比如第一個,就必須加[0],如:
getElementsByName("kkk")[0]
getElementsByTagName("kkk")[0]

其實你從By前面有沒有s就知道哪個要加[0]哪個不用加。

補充一句:你的getElementByTagName是錯的,少了個s
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.