所以我试图创建一个屏幕,它将显示大约50个切换按钮,显示在一个建筑物的监视器上。
我想创建一组图片作为切换使用,这样他们很容易看到。这就是我想要的效果。
http://www.w3schools.com/dhtml/tryit.asp?filename=trydhtml_intro
除了照片,我想让几个灯泡按我的要求开关。
现在这是我的密码。
我可以点击图片来改变我想要的,他们只是不会回去,有什么想法吗?

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
    Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function changeImg(img, newimg) {
    img.src = newimg;
    }


    </script>
    <body>
    <img onclick="changeImg(this, 'staten_uw.jpg')" src="staten_moored.jpg">
    <img onclick="changeImg(this, 'block_uw.jpg')" src="block_moored.jpg">
    </body>
    </html>

最佳答案

它们不会返回,因为代码总是将其更改为uw-uw图像。如果要切换,则函数需要检查当前显示的图像。像这样的:

function changeImg(img) {
    if ( img.src.indexOf("_uw") > 0 ) {
        img.src = img.src.replace("_uw","_moored");
    }
    else {
        img.src = img.src.replace("_moored","_uw");
    }
}

如果您有50x2个不同的图像,命名为“img1_w.jpg”、“img1_umoored.jpg”、“img2_w.jpg”、“img2_umoored.jpg”等,则可以工作。
如果您只有两个图像,但希望每个图像之间有50个按钮可切换,则更容易:
function changeImg(img) {
    if ( img.src == "staten_uw.jpg" ) {
        img.src = "staten_moored.jpg";
    }
    else {
        img.src = "staten_uw.jpg";
    }
}

其他答案也很简单地表明了这一点。
不管是哪种方式,HTML都应该更改为如下内容:
<img onclick="changeImg(this)" src="block_moored.jpg">

关于javascript - 多个Onclick图片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11583482/

10-09 17:27
查看更多