0

所以我遵循了一个非常严格的视频教程,通过 JS 在你的网页中添加时钟。我有完全相同的代码,但它不适用于我的。有什么建议么?谢谢!

这是我的代码:

<body>

    <div id="clockDisplay">00:00</div>


<!-- JAVASCRIPT starts here -------------------------------------------------------->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" language="JavaScript">

$(window).load(function renderTime() {
    var currentTime = new Date () ;
    var diem = "AM" ;
    var h = currentTime.getHours() ;
    var m = currentTime.getminutes() ;
    var s = currentTime.getSeconds() ;

    if (h == 0) {
        h = 12;
    } else if (h > 12) {
    h = h -12;
    diem = "PM" ;
    }
    if (h < 10) {
        h = "0" + h;
    }
    if (m < 10) {
        m = "0" + m;
    }
    if (s < 10) {
        s = "0" + s;
    }

    var myClock = document.getElementyID('clockDisplay');
    myClock.textContent = h + ":" + m + ":" + s + " " + diem;
    setTimeout(renderTime()' ,1000) ;
};
renderTime() ;

</script>

<!-- JAVASCRIPT ends here --------------------------------------------------------->

</body>

4

4 回答 4

2

您的 setTimeout 代码中有语法错误(引号不匹配)。永远不要使用字符串作为 setTimeout 的第一个参数。

setTimeout(renderTime, 1000);

$(window).load()如果将 Javascript 代码放在元素之后,则不需要id="clockDisplay"

function renderTime() {
   ...
}
renderTime();

这些也需要改变。

getElementById()
getMinutes()
于 2013-02-19T16:32:56.770 回答
0

不知道你下面的教程,但我会改变这一行:

myClock.textContent = h + ":" + m + ":" + s + " " + diem;

对此:

myClock.innerHTML = h + ":" + m + ":" + s + " " + diem;
于 2013-02-19T16:31:38.790 回答
0

这里要解决的事情太多,这是我的代码:

<body>

    <div id="clockDisplay">00:00</div>


<!-- JAVASCRIPT starts here -------------------------------------------------------->

<script type="text/javascript" language="JavaScript">

function renderTime() {
    var currentTime = new Date () ;
    var diem = "AM" ;
    var h = currentTime.getHours() ;
    var m = currentTime.getMinutes() ;
    var s = currentTime.getSeconds() ;

    if (h == 0) {
        h = 12;
    } else if (h > 12) {
    h = h -12;
    diem = "PM" ;
    }
    if (h < 10) {
        h = "0" + h;
    }
    if (m < 10) {
        m = "0" + m;
    }
    if (s < 10) {
        s = "0" + s;
    }

    var myClock = document.getElementById('clockDisplay');
    myClock.textContent = h + ":" + m + ":" + s + " " + diem;

}
window.onload = renderTime;
setInterval(renderTime ,1000) ;

</script>

<!-- JAVASCRIPT ends here --------------------------------------------------------->

</body>

要查看详细信息修复,请访问:已修复问题详细信息链接

于 2013-02-19T16:44:45.507 回答
0

我在我的个人开发网站上创建了一个数字时钟,但它没有动画......我的 javascript 如下:

下午 7 点 23 分 45 秒
<script>
function webClock() {
  var pos = "PM";
  var pickTime = new Date();
  var h = pickTime.getHours();
  var m = pickTime.getMinutes();
  var s = pickTime.getSeconds();

  if(h == 0){
    h = 12;

  }else if(h>12){
     h = h-12;
     pos="AM";
  }

 if(h<10){
      h = "0" + h;

  }
  if(m<10){
      m = "0" + m;

  }
  if(s<10){
      s = "0" + s;

  } 

   document.getElementById('MyClock').innerHTML= h + ":" + m + ":" + s + " " +"pos"; 

   setTimeout(webClock, 500);
}
webClock();

}
</script>
于 2019-11-01T13:54:46.190 回答