求教JS高手,为什么document.getElementById("time").innerHTML=out;这句没反应?
functiontellTime(){varout="";varnow=newDate();out+="<br/>Date:"+now.getDate();out+="<...
function tellTime(){
var out="";
var now=new Date();
out +="<br/>Date:"+now.getDate();
out +="<br/>Month:"+now.getMonth();
out +="<br/>Hours:"+now.getHours();
out +="<br/>Minutes:"+now.getMinutes();
document.write(out);
document.getElementById("time").innerHTML=out;
}
</script>
<script>
tellTime();
</script>
<div id="time">tellTime</div> 展开
var out="";
var now=new Date();
out +="<br/>Date:"+now.getDate();
out +="<br/>Month:"+now.getMonth();
out +="<br/>Hours:"+now.getHours();
out +="<br/>Minutes:"+now.getMinutes();
document.write(out);
document.getElementById("time").innerHTML=out;
}
</script>
<script>
tellTime();
</script>
<div id="time">tellTime</div> 展开
2个回答
展开全部
页面渲染是从上往下的,你调用的时候,页面还获取不到到你的dom元素,改成这样试试:
function tellTime(){
var out="";
var now=new Date();
out +="<br/>Date:"+now.getDate();
out +="<br/>Month:"+now.getMonth();
out +="<br/>Hours:"+now.getHours();
out +="<br/>Minutes:"+now.getMinutes();
document.write(out);
document.getElementById("time").innerHTML=out;
}
</script>
<div id="time">tellTime</div>
<script>
tellTime();
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询