Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
clientHeight不能在JavaScript中工作?_Javascript_Html - Fatal编程技术网

clientHeight不能在JavaScript中工作?

clientHeight不能在JavaScript中工作?,javascript,html,Javascript,Html,我正在学习JavaScript并创建了一个倒计时HTML页面,其中JavaScript代码似乎返回了错误的clientHeight。我想在正文的中间(垂直方向)显示我的倒计时HTML,即使在重新调整浏览器大小时也是如此。请告诉我哪里错了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <

我正在学习JavaScript并创建了一个倒计时HTML页面,其中JavaScript代码似乎返回了错误的
clientHeight
。我想在正文的中间(垂直方向)显示我的倒计时HTML
,即使在重新调整浏览器大小时也是如此。请告诉我哪里错了

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CountDown Timer</title>
<script type="text/javascript" >
function countdown(){
    if(isNaN(document.getElementById("time").value))
    {
       document.getElementById("time").value = "Enter Valid Number";
       return;
    }
    else
    {       
       var i = document.getElementById("time").value;
       var j=setInterval(function (){document.getElementById("new").innerHTML = i; i--;
       if(i == -2){
         document.getElementById("new").innerHTML = "Welcome";   
         clearInterval(j);}},1000);
       }
    }

    function heightadjust(){
       document.getElementById("main1").style.top = ((document.body.clientHeight/2)+54).toString() + "px";
    }

</script>
</head>

<body onresize="heightadjust();" onload="heightadjust();">
<div style="margin:0 auto;" id="main1">
   <center>
     <div id="new" style="display:inline-block; padding:3px; font-size:60px; text-align:center; border:3px solid #000000; background-color:#CC3300; color:#FFFFFF; border-radius:6px;">Enter Time Below</div>
     <br />
     <input type="text" id="time" onfocus="this.value = ''; this.style.color = '#000000';" value="Enter Time here" style="color:#999999;" />
     <button onclick="countdown();" >Start</button>
   </center>
</div>

</body>
</html>

倒数计时器
函数倒计时(){
if(isNaN(document.getElementById(“time”).value))
{
document.getElementById(“time”).value=“输入有效数字”;
返回;
}
其他的
{       
var i=document.getElementById(“时间”).value;
var j=setInterval(函数(){document.getElementById(“new”).innerHTML=i;i--;
如果(i==-2){
document.getElementById(“新”).innerHTML=“欢迎”;
clearInterval(j);}},1000);
}
}
函数高度调整(){
document.getElementById(“main1”).style.top=((document.body.clientHeight/2)+54.toString()+“px”;
}
在下面输入时间

开始

请给我一些提示,如何在浏览器重新调整大小的情况下保持
垂直居中。

你不需要JS

#in_the_middle {
  position:fixed;
  left:50%;
  top:100%;
  width:200px;
  margin-left:-100px; /* MUST be equal to width * -0.5 */
  height:50px;
  margin-top:-25px; /* SHOULD equal height * -0.5, but can vary for different fx */
  line-height:50px; /* Remove if there may be more than one line inside */
}

如果宽度和高度是动态的呢?我想用JavaScript的方式来做,因为我正在学习它,(也请不要告诉我J-Query的方式)document.body.clientHeight没有返回正确的值,在任何新的JavaSript格式中都是禁止的(我不确定)。现在我发现document.body.clientHeight返回0/null值,这个财产是被禁止的吗?更恰当的问题是。