简体   繁体   English

Javascript-使用按钮显示/隐藏图片

[英]Javascript - Show/hide pictures with buttons

I would like to improve my script so I can have more buttons which can show one image each. 我想改善自己的脚本,以便可以有更多按钮,每个按钮可以显示一张图像。 (I need 49 buttons). (我需要49个按钮)。 At the moment I only have two well working buttons. 目前,我只有两个运行良好的按钮。 How should I write my script below so I can make 49 buttons? 我应该如何在下面编写脚本,以便创建49个按钮?

JavaScript 的JavaScript

<script type="text/javascript" language="javascript">
    function showHide() {
        var ele = document.getElementById("showHideDiv");
        var ele1 = document.getElementById("showHideDiv1");
        ele1.style.display = "none";
        if(ele.style.display == "block") {
                ele.style.display = "none";             
          }
        else {
            ele.style.display = "block";            
        }
    }

function showHide1() {
    var ele = document.getElementById("showHideDiv");
    var ele1 = document.getElementById("showHideDiv1");
    ele.style.display = "none";
    if(ele1.style.display == "block") {
            ele1.style.display = "none";
      }
    else {
        ele1.style.display = "block";
    }
}
</script>

HTML 的HTML

<button onclick="return showHide();">box1</button>
<button onclick="return showHide1();">box2</button>
<div id="showHideDiv" style="display:none;"><img  src="1.gif" height="280" width="120"/></div>
<div id="showHideDiv1" style="display:none;"><img  src="2.gif" height="280" width="120"/></div>

The showHide() can receive a parameter with the div id. showHide()可以接收具有div ID的参数。

function showHide(my_id) {
    var ele = document.getElementById("showHideDiv" + my_div);
    if(ele.style.display == "block") {
            ele.style.display = "none";             
    }
    else {
        ele.style.display = "block";            
    }
}

What lacks in this code is to hide all the other divs. 该代码中缺少的是隐藏所有其他div。

Well, this is another approach, using jQuery (I know, I know, but I just can't make it work on pure javascript). 好吧,这是使用jQuery的另一种方法(我知道,我知道,但是我无法使其在纯JavaScript上运行)。 I've used the following markup: 我使用了以下标记:

<div id="template">
    <button class="toggleButton"></button>
    <div class="showHideDiv"><img src="" height="280" width="120"/></div>
</div>
<div id="main">
</div>

Div template is used as template for other divs. Div template用作其他div的模板。 Here is javascript code: 这是JavaScript代码:

var main = $('#main');
// copy template to make 49 images.
var template = $('#template');
for (var i= 0; i < 49; i++) {
    var div = template.clone();
    var img = main.find('img');
    var button = div.find('button');
    img.attr('src', i + '.gif');
    button.text('box'+(i+1));   
    main.append(div.html());    
}
// add toggle event handler
$(document).on("click", ".toggleButton", function(e) {
    $('.showHideDiv').hide();
    $(e.target).next('.showHideDiv').show();
});

Here is demo . 这是演示 It has a lot of errors in console, since there are no real images there, but that should be ok in real application. 它在控制台中有很多错误,因为那里没有真实的图像,但是在实际的应用程序中应该没问题。

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

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