两个JavaScript不能一起运行

时间:2018-07-23 12:03:26

标签: javascript html

我在一个html中有两个javascript,但是它们没有一起运行,但是分别运行是。也许有人知道为什么?问题是什么?谢谢!

第一个javascript: 这是时钟。

    <span style='position:absolute;z-index:1;
    left:484px;top:440px;font-family: arial; font-weight: bold;font-size: 65';
    <div id="txt"></div></span>
<body onload="startTime()">


<script>
function startTime() {
    var today = new Date();
    var h = today.getUTCHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =
    h + ":" + m + ":" + s;
    var t = setTimeout(startTime, 500);
}
function checkTime(i) {
    if (i < 10) {i = "0" + i};
    return i;
}
</script>

第二个javascript: 这是一个闪烁的图像。

<script>
var tId, images = [],
  isBlinking = false;
currImg = 0,

images[0] = new Image(); images[0].src = "a.gif";
images[1] = new Image(); images[1].src = "b.gif";
images[2] = new Image(); images[2].src = "c.gif";

window.onload = function() {
  var img = document.getElementById("ID");

  img.onclick = function() {
    if (isBlinking) {
      clearInterval(tId);
      isBlinking = false;
      currImg = currImg == 0 ? 1 : 0;
      img.src = images[currImg].src;

    } else {
      isBlinking = true;
      tId = setInterval(function() {
        var src = document.getElementById("ID").src;
        // blink
        document.getElementById("ID").src = src == images[currImg].src ? images[2].src : images[currImg].src;
      }, 300);
    }

  }
}
</script>
<img id="ID" src="a.gif" />

2 个答案:

答案 0 :(得分:1)

请参阅下面的更改。做文件比较。

<span style='position:absolute;z-index:1;
left:484px;top:440px;font-family: arial; font-weight: bold;font-size: 65'>
</span>
<div id="txt"></div>
<img id="ID" src="a.gif" />

<script>

    startTime();

    function startTime() {
        var today = new Date();
        var h = today.getUTCHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        m = checkTime(m);
        s = checkTime(s);
        document.getElementById('txt').innerHTML =
            h + ":" + m + ":" + s;
        var t = setTimeout(startTime, 500);
    }
    function checkTime(i) {
        if (i < 10) { i = "0" + i };
        return i;
    }
</script>

<script>
    var tId, images = [],
    isBlinking = false;
    currImg = 0,

    images[0] = new Image(); images[0].src = "a.gif";
    images[1] = new Image(); images[1].src = "b.gif";
    images[2] = new Image(); images[2].src = "c.gif";

    window.onload = function () {
        var img = document.getElementById("ID");

        img.onclick = function () {
            if (isBlinking) {
                clearInterval(tId);
                isBlinking = false;
                currImg = currImg == 0 ? 1 : 0;
                img.src = images[currImg].src;

            } else {
                isBlinking = true;
                tId = setInterval(function () {
                    var src = document.getElementById("ID").src;
                    // blink
                    document.getElementById("ID").src = src == images[currImg].src ? images[2].src : images[currImg].src;
                }, 300);
            }

        }
    }
</script>

答案 1 :(得分:0)

尝试window.onload = startTime;

var tId, images = [],
            isBlinking = false;
        currImg = 0,

            images[0] = new Image(); images[0].src = "a.gif";
        images[1] = new Image(); images[1].src = "b.gif";
        images[2] = new Image(); images[2].src = "c.gif";

        
            var img = document.getElementById("ID");

            img.onclick = function () {
                if (isBlinking) {
                    clearInterval(tId);
                    isBlinking = false;
                    currImg = currImg == 0 ? 1 : 0;
                    img.src = images[currImg].src;

                } else {
                    isBlinking = true;
                    tId = setInterval(function () {
                        var src = document.getElementById("ID").src;
                        // blink
                        document.getElementById("ID").src = src == images[currImg].src ? images[2].src : images[currImg].src;
                    }, 300);
                }

            }
        

        function startTime() {
            var today = new Date();
            var h = today.getUTCHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            m = checkTime(m);
            s = checkTime(s);
            document.getElementById('txt').innerHTML =
                h + ":" + m + ":" + s;
            var t = setTimeout(startTime, 500);
        }
        function checkTime(i) {
            if (i < 10) { i = "0" + i };
            return i;
        }
        window.onload = startTime;
<div id="txt"></div>
<img id="ID" src="a.gif" />