1

我想每隔一秒隐藏并看到一些html中的元素。我在 JavaScript 中有这段代码。hello() 从主 html 调用 onload。然后它使用调用函数启动计时器!我认为它每秒钟都会改变它显示的内容。

function hello() {

myTimer();


}
var indexx = 0;
function myTimer(){
var myVar1=setInterval(function(){go1()},1000);
}
function go1()
{

 if(indexx==0)
    {
        var results = document.getElementById("water_2_5");
        results.setAttributeNS(null,"visibility","hidden");
        var results = document.getElementById("water_2_1");
        results.setAttributeNS(null,"visibility","visible");

        indexx+=1;

    }   else
    if(indexx==1)
    {
        results = document.getElementById("water_2_1");
        results.setAttributeNS(null,"visibility","hidden");
        //
        var results = document.getElementById("water_2_7");
        results.setAttributeNS(null,"visibility","visible");


        indexx+=1;

    }
    if(indexx==2)
    {
    results = document.getElementById("water_2_7");
        results.setAttributeNS(null,"visibility","hidden");
        //
        var results = document.getElementById("water_2_6");
        results.setAttributeNS(null,"visibility","visible");


        indexx+=1;

    }
    if(indexx==3)
    {
    results = document.getElementById("water_2_6");
        results.setAttributeNS(null,"visibility","hidden");
        //
        var results = document.getElementById("water_2_5");
        results.setAttributeNS(null,"visibility","visible");

                indexx=0;
        }

}

但它只显示给我,第一部分和最后一部分?有人可以帮我如何实现吗?

答案是:

function hello() {

myTimer();


}
var indexx = 0;
function myTimer(){
var myVar1=setInterval(function(){go1()},1000);
}
function go1()
{

 if(indexx==0)
    {
        var results = document.getElementById("water_2_5");
        results.setAttributeNS(null,"visibility","hidden");
        var results = document.getElementById("water_2_1");
        results.setAttributeNS(null,"visibility","visible");

        indexx+=1;

    }   else
    if(indexx==1)
    {
        results = document.getElementById("water_2_1");
        results.setAttributeNS(null,"visibility","hidden");
        //
        var results = document.getElementById("water_2_7");
        results.setAttributeNS(null,"visibility","visible");


        indexx+=1;

    } else
    if(indexx==2)
    {
    results = document.getElementById("water_2_7");
        results.setAttributeNS(null,"visibility","hidden");
        //
        var results = document.getElementById("water_2_6");
        results.setAttributeNS(null,"visibility","visible");


        indexx+=1;

    }else 

    {
    results = document.getElementById("water_2_6");
        results.setAttributeNS(null,"visibility","hidden");
        //
        var results = document.getElementById("water_2_5");
        results.setAttributeNS(null,"visibility","visible");

                indexx=0;
        }

}
4

1 回答 1

3

if通过将所有语句(在第一个之后)设为 来对所有语句进行分组else if。这样,块 ( indexx+=1;) 内的增量不会导致 nextif为真。

于 2013-05-15T16:51:36.610 回答