簡體   English   中英

正確的IE6 HTML元素尺寸

[英]Proper IE6 HTML element dimensions

我正在嘗試使用javascript設置元素的寬度和高度以覆蓋整個瀏覽器視口,並且我成功使用了

document.body.clientHeight
但是在IE6中,似乎總是獲得水平和垂直滾動條,因為該元素必須太大。

現在,我真的不想使用特定於瀏覽器的邏輯,而只為IE6從每個維度中減去一個像素或2個像素。 另外,我沒有為此使用CSS(寬度:100%等),因為我需要像素數量。

有誰知道用IE6 +中的元素填充視口的更好方法(顯然也是所有好的瀏覽器)?

編輯:感謝歐文的建議,我敢肯定jQuery會工作。 我應該指定我需要與工具包無關的解決方案。

可能有助於原因...

http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/

<script type="text/javascript">
<!--

 var viewportwidth;
 var viewportheight;

 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }

// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }

 // older versions of IE

 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }
document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
//-->
</script>

您是否考慮過使用jQuery 它將大多數瀏覽器特定的功能抽象為一個通用接口。

var width = $(document).width();
var height = $(document.height();

$('#mySpecialElement').width(width).height(height);

啊哈! 我忘了

document.documentElement.clientLeft
  document.documentElement.clientTop 

在IE中,它們是2,在良好的瀏覽器中是0。 所以用

  var WIDTH =(((document.documentElement.clientWidth-document.documentElement.clientLeft)|| document.body.clientWidth); 
等做的把戲,與HEIGHT類似的想法!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM