简体   繁体   English

创建一个JavaScript库:更改实际图像

[英]Creating a JavaScript gallery: change the actual image

I have tried the various solutions on the site, but I suspect I am doing something wrong with implementing the actual image in the document, or anything. 我已经在站点上尝试了各种解决方案,但是我怀疑在实现文档中的实际图像或其他任何操作时做错了什么。 Thanks in advance for the help! 先谢谢您的帮助!

Here is my current code: 这是我当前的代码:

<html>
<body>

<link type="text/css" rel="stylesheet" href="Stylesheet.css"/>


<img id="teemoimage" src="exampleurl">


<script>
    var leagueofteemo=new Array()
    var counter = 1
    leagueofteemo[1]="exampleurl"
    leageuofteemo[2]="exampleurl"

    function back()
    {
    if (counter = 1)
        {
    counter = leagueofteemo.length;
    }
        else
        {
    counter--;
    }
        document.getElementById("teemoimage").src =leagueofteemo[counter]
    }

    function foward()
    {
        if (counter = leageofteemo.length)
        {
    counter = 1;
    }
        else
        {
    counter++;
    }
        document.getElementById("teemoimage").src = leageuofteemo[counter];
}
</script>

<form>
    <input type="button" onclick="back()" value="Back">
    <input type="button" onclick="foward()" value="Foward">
</form>

There's a few things wrong with your code. 您的代码有些错误。

  • your equality operators are incorrect, 您的平等运算符不正确,
  • js arrays are 0 based. js数组基于0。 So rather start your array at 0. 因此,宁愿将数组从0开始。
  • leagueofteemo variable is spelled incorrectly in some instances Leagueofteemo变量在某些情况下拼写错误

I suggest you use some sort of tool to help you debug your JavaScript eg Firebug. 我建议您使用某种工具来帮助您调试JavaScript,例如Firebug。 Alternatively you can use the developer tools that comes standard with most browsers these days. 另外,您可以使用当今大多数浏览器随附的标准开发人员工具。

I've revised your sample to be syntactically and semantically correct but haven't tested it: 我修改了您的示例,使其在语法和语义上都是正确的,但尚未对其进行测试:

var leagueofteemo = new Array();
var counter = 0;
leagueofteemo[0] = "exampleurl";
leageuofteemo[1] = "exampleurl";

function back() {
    if (counter === 0) {
        counter = leagueofteemo.length;
    }
    else {
        counter--;
    }

    document.getElementById("teemoimage").src = leagueofteemo[counter];
}

function foward() {
    if (counter === leagueofteemo.length-1) {
        counter = 0;
    }
    else {
        counter++;
    }

    document.getElementById("teemoimage").src = leagueofteemo[counter];
}

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

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