繁体   English   中英

将浏览器高度和宽度应用于 div

[英]Apply browser height and width to the div

我可以知道为什么我的 div 块没有显示吗? 谁能给我解释一下? https://jsfiddle.net/sw64j5qc/8/

function deviceSize() {
var myWidth = 0, myHeight = 0;
var block = document.getElementById("test");

if( typeof( window.innerWidth ) == 'number' ) {
  //Non-IE
  myWidth = window.innerWidth;
  myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  //IE 6+ in 'standards compliant mode'
  myWidth = document.documentElement.clientWidth;
  myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  //IE 4 compatible
  myWidth = document.body.clientWidth;
  myHeight = document.body.clientHeight;
}
block.style.width = myWidth;
block.style.height = myHeight;

window.alert( 'Width = ' + myWidth );
window.alert( 'Height = ' + myHeight );

} deviceSize();

您只是将数字传递给属性,而不是传递指定像素的字符串:

block.style.width = myWidth + "px";
block.style.height = myHeight + "px";

您的 div 没有任何内容,如果您尝试会发生什么

<div id="test" style="background-color: #000;">Some content here</div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM