0

嗨,目前我有一些 Javascript 可以让我的图像自动循环遍历存储在数组中的多个图像,但是我怎样才能对 H2 文本对象上的文本做同样的事情呢?

<SCRIPT LANGUAGE = "Javascript">
    var p1 = new Image();
    var p2 = new Image();
    var p3 = new Image();
    var p4 = new Image();

    p1.src="images/223.png";
    p2.src="images/335.png";
    p3.src="images/225.png";
    p4.src="images/226.png";

    var imgArray = new Array (p1, p2, p3, p4);

    var counter = 0;
    var loop = 1;
    setTimeout(scroll_forwards, 2000);

    var end = imgArray.length - 1;

    function scroll_forwards() {
        if (counter == end){
            counter = 0;    
        }
        else {
            counter++;
        }
        document.pic1.src = imgArray[counter].src;
        setTimeout(scroll_forwards, 2000);
    }
</SCRIPT>
4

2 回答 2

1

您只需要执行与图像循环完全相同的操作即可。我更新了你的代码:

<script> // don't use the language attribute

    var imgArray = ["images/223.png","images/335.png","images/225.png","images/226.png"];
    var textArray= ["text1","text2","text3","text4"];
    var counter = 0;
    var h2Tag = document.getElementById("h2TagId"); // change this 
    var imgTag = document.pic1;
    var end = imgArray.length - 1;

    function scroll_forwards() {
        if (counter == end){
            counter = 0;    
        }
        else {
            counter++;
        }
        imgTag.src = imgArray[counter];
        h2Tag.innerHTML = textArray[counter];
        setTimeout(scroll_forwards, 2000);
    }
    setTimeout(scroll_forwards, 2000);
</script>
于 2013-10-13T05:48:27.940 回答
0

我不知道为什么昨晚我尝试它时它没有工作,但我只是为文本重新编写了 Javascript,现在它正在工作......无论如何,谢谢,这是代码,所以其他人都知道。

<h2 id="imgh2">Change this</h2>



<SCRIPT LANGUAGE = "Javascript">
    var p1 = new Image();
    var p2 = new Image();
    var p3 = new Image();
    var p4 = new Image();

    var t1;
    var t2;
    var t3;
    var t4;

    p1.src="images/1232.png";
    p2.src="images/12312.png";
    p3.src="images/1241t.png";
    p4.src="images/12455.png";

    t1 = "Test 1";
    t2 = "Test 2";
    t3 = "Test 3";
    t4 = "Test 4";

    var imgArray = new Array (p1, p2, p3, p4);
    var textArray = new Array (t1, t2, t3, t4);

    var counter = 0;
    var loop = 1;
    setTimeout(scroll_forwards, 2000);

    var end = imgArray.length - 1;

    function scroll_forwards() {
        if (counter == end){
            counter = 0;    
        }
        else {
            counter++;
        }
        document.pic1.src = imgArray[counter].src;
        document.getElementById("imgh2").innerHTML = textArray[counter];
        setTimeout(scroll_forwards, 2000);
    }
</SCRIPT>
于 2013-10-13T05:59:10.093 回答