简体   繁体   English

如何使用上一个/下一个按钮循环显示图像?

[英]How do I loop through images with Previous/Next buttons?

I was able to get the images to loop through but now when I click on either the 'Previous' or 'Next' link, my file folder where I have my images stored pops up, every time. 我能够让图像循环播放,但现在当我点击“上一个”或“下一个”链接时,每次都会弹出我存储图像的文件夹。 Any idea as to why this is happening or how to stop it? 知道为什么会发生这种情况或如何阻止它?

THE SCRIPT 剧本

<script type="text/javascript">
            /* <![CDATA[ */
                function changeDisplay(curLine) {
                    curLine.style.color = "red";
                    curLine.style.fontWeight = "bold";
                }

                function restoreDisplay(curLine) {
                    curLine.style.color = "blue";
                    curLine.style.fontWeight = "normal";
                }

                var curImage = 1;

                function changeImage(which) {
                    for (var i=0; i < 5; ++i) {
                        document.getElementById("image"+i).style.display = "none";
                    }
                    if (which < 1) 
                        which = 5;
                    if (which > 5)
                        which = 1;
                    document.getElementById("image"+which).style.display = "block";
                    curImage = which;
                }
            /* ]]> */
            </script>

THE HTML HTML

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <link type="text/css" rel="stylesheet" href="project.css" />

    </head>
    <body>
        <div>
            <table style="border:none" class="menu">
                <tr>
                    <td><a href="Index.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Home</a></td>
                    <td class="menupipe">|</td>
                    <td><a href="Specials.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Specials</a></td>
                    <td class="menupipe">|</td>
                    <td><a href="Tools.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Tools</a></td>
                    <td class="menupipe">|</td>
                    <td><a href="Prices.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Prices</a></td>
                    <td class="menupipe">|</td>
                    <td><a href="Order.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Shop</a></td>
                </tr>
            </table>
        </div>
        <div>
            <p style="font-weight:bold; font-size:20px">With winter approaching, every homeowner wants to be prepared for the snow and ice! We are now running a sale on a few items that will help handle the harsh winter conditions. Take a look at our prices and make sure you stock up. Click on the images below for more information.</p>
        </div>
        <div id="image" style="display:block">
            <img src="images/deicer.jpg" height="300" width="300" alt="deicer" />
        </div>
        <div id="image" style="display:none">
            <img src="images/snowblower.jpg" height="300px" width="300px" alt="snowblower" />
        </div>
        <div id="image" style="display:none">
            <img src="images/icemelt.jpg" height="300px" width="250px" alt="icemelt" />
        </div>
        <div id="image" style="display:none">
            <img src="images/shovel.jpg" height="225px" width="225px" alt="shovel" />
        </div>
        <div id="image" style="display:none">
            <img src="images/scraper.jpg" height="124" width="124" alt="scraper" />
        </div>
        <button type="button" onclick="changeImage(curImage - 1)">Previous</button>
        <button type="button" onclick="changeImage(curImage + 1)">Next</button> 
    </body>
</html>

Add index (1 - 5) to all your image divs like: 为所有图像div添加索引(1 - 5),如:

<div id="image1" style="display:block">

Change index in the loop as well to have everywhere 1 - 5 . 在循环中改变索引以及到处都是1 - 5 Or change everything to 0 - 4 . 或者将所有内容更改为0 - 4

all your image containers 所有图像容器

<div id="image" style="display:none">

have the same id and don't correspond to the format image# 具有相同的ID并且不对应格式图像#

Also change the following loop from 同时更改以下循环

for (var i=0; i < 5; ++i) {
     document.getElementById("image"+i).style.display = "none";
    }

to

for (var i=0; i < 5; ++i) {
         document.getElementById("image"+ (i + 1)).style.display = "none";
        }

I got it to work by adding 1,2,3,4 to each of your image containers.. 我通过在每个图像容器中添加1,2,3,4来实现它。

My version is below 我的版本如下

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <link type="text/css" rel="stylesheet" href="project.css" />
        <script type="text/javascript">
        /* <![CDATA[ */
            function changeDisplay(curLine) {
                curLine.style.color = "red";
                curLine.style.fontWeight = "bold";
            }

            function restoreDisplay(curLine) {
                curLine.style.color = "blue";
                curLine.style.fontWeight = "normal";
            }

            var curImage = 1;

            function changeImage(which) {
                for (var i=0; i < 5; ++i) {
                    document.getElementById("image"+i).style.display = "none";
                }
                if (which < 1) 
                    which = 5;
                if (which > 5)
                    which = 1;
                document.getElementById("image"+which).style.display = "block";
                curImage = which;
            }
        /* ]]> */
        </script>
    </head>
    <body>
        <div>
            <table style="border:none" class="menu">
                <tr>
                    <td><a href="Index.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Home</a></td>
                    <td class="menupipe">|</td>
                    <td><a href="Specials.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Specials</a></td>
                    <td class="menupipe">|</td>
                    <td><a href="Tools.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Tools</a></td>
                    <td class="menupipe">|</td>
                    <td><a href="Prices.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Prices</a></td>
                    <td class="menupipe">|</td>
                    <td><a href="Order.html" class="menuicon" onmouseover="changeDisplay(this)" onmouseout="restoreDisplay(this)">Shop</a></td>
                </tr>
            </table>
        </div>
        <div>
            <p style="font-weight:bold; font-size:20px">With winter approaching, every homeowner wants to be prepared for the snow and ice! We are now running a sale on a few items that will help handle the harsh winter conditions. Take a look at our prices and make sure you stock up. Click on the images below for more information.</p>
        </div>
        <div id="image0" style="display:block">
            <img src="http://placehold.it/350/ffffff/000000">
        </div>
        <div id="image1" style="display:none">
            <img src="http://placehold.it/350/ffffff/000000">
        </div>
        <div id="image2" style="display:none">
            <img src="http://placehold.it/350/ffffff/000000">
        </div>
        <div id="image3" style="display:none">
            <img src="http://placehold.it/350/ffffff/000000">       
        </div>
        <div id="image4" style="display:none">
            <img src="http://placehold.it/350/ffffff/000000">
        </div>
        <button type="button" onclick="changeImage(curImage - 1)">Previous</button>
        <button type="button" onclick="changeImage(curImage + 1)">Next</button> 
    </body>
</html>

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

相关问题 如何使用jQuery将下一个和上一个按钮添加到我的幻灯片? - How do I add next and previous buttons to my slideshow with jQuery? 如何循环遍历按钮数组,并且只有在用户单击数组中的下一个按钮时才继续循环 - How do I loop through an array of buttons and only continue looping if the user clicks the next button in the array 使用上一个和下一个按钮在javascript中循环浏览图像 - Using previous and next buttons to cycle through images in javascript 试图使“下一个”和“上一个”按钮在单击时循环导航 - Trying to make “next” and “previous” buttons that loop through navigation onclick 添加图像的上一个和下一个按钮 - Adding previous and next buttons for images 如何实现下一个/上一个按钮来循环显示HTML中的表格数据 - How would I implement Next/Previous Buttons to cycle through tabular data in HTML “上一个”和“下一个”按钮不起作用 - The “previous” and “next” buttons do not work 如何将下一个和上一个按钮添加到我的 React 幻灯片网站 - How do i add next and previous buttons to my react slideshow website 使用下一个和上一个按钮滚动浏览锚标记 - Scroll through anchor tags with next and previous buttons 下一个和上一个按钮逐步浏览Bootstrap选项卡 - Next and Previous buttons step through Bootstrap Tabs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM