0

我正在用 HTML5 制作一个图像查看器,我想在 body 或 head 上写一个脚本标签,但它不起作用。以下是主脚本的摘录:

function play() {
    document.getElementsByClassName("play")[0].setAttribute("class", "play active");
    document.getElementsByClassName("pause")[0].setAttribute("class", "pause");
    var body = document.body.innerHTML;
    body.innerHTML=body + "<script id='playpause' src='res/playpause.js'></script>";
}

这是我正在尝试编写的脚本:

var slideshow = setInterval("forward();",5000);

function pause() {
    document.getElementsByClassName("play")[0].setAttribute("class", "play");
    document.getElementsByClassName("pause")[0].setAttribute("class", "pause active");
    window.clearInterval(slideshow);
    var playpause = document.getElementById("playpause");
    playpause.parentNode.removeChild(playpause);
}

这是HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Image viewer</title>
        <link rel="stylesheet" href="res/style.css" />
        <script src="res/script.js"></script>
    </head>
    <body onload="play()">
        <div class="img">
            <img title="Hover over me to un-darken" alt="Error displaying image" src="images/1.jpg" id="img" data-number="1" />
        </div>
        <div title="Backward" class="backward" onclick="backward()"></div>
        <div title="Forward" class="forward" onclick="forward()"></div>
        <div title="Play" class="play active" onclick="play()"></div>
        <div title="Pause" class="pause" onclick="pause()"></div>
        <div title="Go to beginning" class="stop" onclick="stop()"></div>
    </body>
</html>

有任何想法吗?

4

1 回答 1

0

也许是这样的:

var s = document.createElement('script');
s.setAttribute("type", "text/javascript");
s.setAttribute("src", "res/playpause.js");
var head = document.getElementsByTagName('head')[0];
head.appendChild(s);
于 2013-09-02T13:24:47.920 回答