创建手动幻灯片时出错

时间:2017-06-20 05:38:03

标签: javascript html

出于某种原因,我在我的javascript文件中的一行上遇到以下错误(我在下面指出了这一行)。这是我的HTML:

<div class="slideshowproject">
    <button id="rightbutton" onclick=manualslideshow("rightbutton,projectslides1")>></button>
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 1-1.jpg">
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 2-1.jpg">
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 3-1.jpg">
    <button id="leftbutton" onclick=manualslideshow("leftbutton","projectslides1")><</button>
    <script>manualslideshow("","projectslides1")</script>
</div>

和我的javascript:

var imagenumber=0;
function manualslideshow(buttonid,slideshowclass){
    var pictures=document.getElementsByClassName(slideshowclass);
    for (i=0;i<pictures.length;i++){
        pictures[i].style.display="none";
    }
    if (buttonid=="rightbutton"){
        imagenumber++;
    }
    else if (buttonid=="leftbutton"){
        imagenumber--;
    }
    if (imagenumber>pictures.length){
        imagenumber=1;
    }
    if (imagenumber<1){
        imagenumber=pictures.length;
    }
    pictures[imagenumber-1].style.display="block";
}

由于某种原因,&#34; .style.display =&#34; block&#34 ;;&#34;在脚本的最后一行收到错误:

navigationbar.js:68 Uncaught TypeError: Cannot read property 'style' of undefined
    at manualslideshow (navigationbar.js:68)
    at HTMLButtonElement.onclick (clinic.html:42)

Javascript第68行是最后一个闭合花括号之前的最后一行,html中第42行是&#34;右键按钮&#34; line(复制的HTML中的第二行)

感谢您的所有帮助和时间。

2 个答案:

答案 0 :(得分:2)

查看代码时,我可以在从Html文件调用'manualslideshow'方法时看到错误。 (即第42行)。 请看下面的代码。我改变了 (“rightbutton,projectslides1”)(“rightbutton”,“projectslides1”)

<div class="slideshowproject">
    <button id="rightbutton" onclick=manualslideshow("rightbutton","projectslides1")>></button>
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 1-1.jpg">
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 2-1.jpg">
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 3-1.jpg">
    <button id="leftbutton" onclick=manualslideshow("leftbutton","projectslides1")><</button>
    <script>manualslideshow("","projectslides1")</script>
</div>

答案 1 :(得分:1)

调用manualslideshow函数时出现了一些错误:

var imagenumber=0;

function manualslideshow(buttonid,slideshowclass){
    var pictures=document.getElementsByClassName(slideshowclass);
    for (i=0;i<pictures.length;i++){
        pictures[i].style.display="none";
    }
    if (buttonid=="rightbutton"){
        imagenumber++;
    }
    else if (buttonid=="leftbutton"){
        imagenumber--;
    }
    if (imagenumber>pictures.length){
        imagenumber=1;
    }
    if (imagenumber<1){
        imagenumber=pictures.length;
    }
    pictures[imagenumber-1].style.display="block";
}

manualslideshow("","projectslides1")
<div class="slideshowproject">
    <button id="rightbutton" onclick=manualslideshow("rightbutton","projectslides1")>></button>
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 1-1.jpg">
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 2-1.jpg">
    <img class="projectslides1" src="Project Images\\HarrisonHealthClinic\\Render 3-1.jpg">
    <button id="leftbutton" onclick=manualslideshow("leftbutton","projectslides1")><</button>

</div>