脚本不再更新页面

时间:2016-10-23 15:50:06

标签: javascript html

我有以下html页面,它在随机位置显示图片并每10秒更新一次:

<!DOCTYPE html>
<html>
<head>
<title>Domoos | Screen saver screen</title>
<meta http-equiv="refresh" content="10">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<meta http-equiv="pragma" content="no-cache">
<link rel="stylesheet" type="text/css" href="css/mystyle_saver.css" />
<script type="text/javascript" src="scripts/date_time.js"></script>
<script type="text/javascript">
    function init() {
    var xmin = 0;
    var xmax = 890;
    var ymin = 0;
    var ymax = 430;
    var xCoord = Math.floor((Math.random()*xmax)+xmin);
    var yCoord = Math.floor((Math.random()*ymax)+ymin);
    var xCoordStr = xCoord.toString() + "px";
    var yCoordStr = yCoord.toString() + "px";
    document.getElementById("randomPlacement").style.left = xCoordStr;
    document.getElementById("randomPlacement").style.top = yCoordStr;
}
</script>
</head>
<body onload="init();">
<div style="position:absolute" id="randomPlacement">
<p><a href="index.html"><img src="assets/pictures/texte_sortie_veille.png" alt ="" style="width:60px;height:60px;"></a></p>
</div>
</body>
</html>

效果很好。现在,我想添加两个'div'标签,这样我就可以显示日期和时间。所以,我更新了body标签,如下所示(页面的其余部分保持不变):

<body onload="init();">
 <div style="position:absolute" id="randomPlacement">
 <p><a href="index.html"><img src="assets/pictures/texte_sortie_veille.png" alt ="" style="width:60px;height:60px;"></a></p>
</div>
<div id="date">
    <script type="text/javascript">window.onload = getDate('date');</script>
</div>
<div id="time">
    <script type="text/javascript">window.onload = getTime('time');</script>
</div>
</body>

这样做的问题是图像永远不会在随机位置显示。我错过了什么,希望如何解决这个问题?我在Chrome中添加了控制台的摘录。

非常感谢你的帮助。

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

替换

  <div id="date">
    <script type="text/javascript">window.onload = getDate('date');</script>
</div>
<div id="time">
    <script type="text/javascript">window.onload = getTime('time');</script>
</div>

    
<div id="time">

</div>

然后在init()

 function init() {
        var xmin = 0;
        var xmax = 890;
        var ymin = 0;
        var ymax = 430;
        var xCoord = Math.floor((Math.random()*xmax)+xmin);
        var yCoord = Math.floor((Math.random()*ymax)+ymin);
        var xCoordStr = xCoord.toString() + "px";
        var yCoordStr = yCoord.toString() + "px";
        document.getElementById("randomPlacement").style.left = xCoordStr;
        document.getElementById("randomPlacement").style.top = yCoordStr;
        document.getElementById("date").innerhtml=getDate('date');
document.getElementById("time").innerhtml=gettime('time');
    }