javascript学习

html:

<html>

   <head>

      <script src="text.js">    //链接javas

  </head>

<body onload="startTime()">    调用javascript中的startTime()方法

           <div id="header">
               
                <!--超链接-->
                <a title="百度一下你就知道" href="http://www.baidu.com">百度一下你就知道</a>
                <input type="button" onclick="changecolor()" value="changecolor"/>
                <div id="txt">
                </div>
            </div>

</html>

javascript:

           //改变id为header的背景颜色为黑色

           function changecolor()
          {
                 $('header').style.backgroundColor = 'black';
          }
           function $(id)
         {
                  return document.getElementById(id);
         }

          //显示一个时钟在header里

          function startTime()
        {
                   var today=new Date()
                   var h=today.getHours()//得到当前的小时
                   var m=today.getMinutes()//得到当前的分钟
                   var s=today.getSeconds()//得到当前的秒数
                  // add a zero in front of numbers<10
                  m=checkTime(m)
                  s=checkTime(s)
                  document.getElementById('txt').innerHTML=h+":"+m+":"+s
                  t=setTimeout('startTime()',500)每隔0.5秒执行一次startTime()方法
         }

          //如果分钟和秒数的数字没有超过10前一位的位置用0补充
          function checkTime(i)
        {
                  if (i<10)
                  {i="0" + i}
                   return i
          }

原文地址:https://www.cnblogs.com/liushuang/p/2862278.html