繁体   English   中英

JavaScript阵列在单击时显示不同的图片无法正常工作

[英]JavaScript array to display different pictures on click not functioning as expected

我目前正在这样做,以便当您单击一个数字时,它上面的图片将发生变化,最终这些图片将根据其他输入进行实时更新(如果有人有任何好的想法,即使这是另一个主题) ),这样的想法是您可以浏览并单击其中一张图片以获得更好的外观。

我已经做好了外发工作,但是我也在努力使外来工作正常。

他们俩都在工作,我想我已经在某个地方取得了成功。

有人帮忙吗?

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
function changeIt(objName)
{
    //The image object accessed through its id we mentioned in the DIV block which is going to be visible currently
    var obj = document.getElementById(objName);

    //An array that hold the IDs of images that we mentioned in their DIV blocks
    var objId = new Array();

    //Storing the image IDs into the array starts here
    objId[0] = "limage1";
    objId[1] = "limage2";
    objId[2] = "limage3";
    objId[3] = "limage4";
    objId[4] = "limage5";
    //Storing the image IDs into the array ends here

    //A counter variable going to use for iteration
    var i;

    //A variable that can hold all the other object references other than the object which is going to be visible
    var tempObj;

    //The following loop does the display of a single image based on its ID. The image whose ID we passed into this function will be the
    //only image that is displayed rest of the images will be hidden based on their IDs and that part has been handled by the else part
    //of the if statement within this loop.
    for(i=0;i<objId.length;i++)
    {
        if(objName == objId[i])
        {
            obj.style.display = "block";
        }
        else
        {
            tempObj = document.getElementById(objId[i]);
            tempObj.style.display = "none"; 
        }
    }
    return; 
}
</script>

<script type="text/javascript">
function changeIt(objName)
{
    //The image object accessed through its id we mentioned in the DIV block which is going to be visible currently
    var obj = document.getElementById(objName);

    //An array that hold the IDs of images that we mentioned in their DIV blocks
    var objId = new Array();

    //Storing the image IDs into the array starts here
    objId[0] = "rimage1";
    objId[1] = "rimage2";
    objId[2] = "rimage3";
    objId[3] = "rimage4";
    objId[4] = "rimage5";
    //Storing the image IDs into the array ends here

    //A counter variable going to use for iteration
    var j;

    //A variable that can hold all the other object references other than the object which is going to be visible
    var tempObj;

    //The following loop does the display of a single image based on its ID. The image whose ID we passed into this function will be the
    //only image that is displayed rest of the images will be hidden based on their IDs and that part has been handled by the else part
    //of the if statement within this loop.
    for(j=0;j<objId.length;j++)
    {
        if(objName == objId[j])
        {
            obj.style.display = "block";
        }
        else
        {
            tempObj = document.getElementById(objId[j]);
            tempObj.style.display = "none"; 
        }
    }
    return; 
}
</script>

http://jsfiddle.net/9mT8d/1/

您正在调用并定义相同的函数两次。

尝试更改名称。

http://jsfiddle.net/9mT8d/6/

例如

   function changeItOut

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM