按钮单击显示图像

时间:2015-11-01 23:19:01

标签: javascript jquery html css

想知道如何通过点击按钮设置图片的可见性。

<body class="body page-index clearfix">
<img class="image image-1" src="http://exmoorpet.com/wp-content/uploads/2012/08/cat.png">
<img class="image image-2" src="http://exmoorpet.com/wp-content/uploads/2012/08/cat.png">
<button class="_button" >Test</button>
<img class="image image-3" src="http://exmoorpet.com/wp-content/uploads/2012/08/cat.png">

.image {
    display: block;
    visibility: hidden;
    height: auto;
    overflow: hidden;
}

小提琴:https://jsfiddle.net/cz5yyu83/

2 个答案:

答案 0 :(得分:2)

首先在头标记中包含jQuery库:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

然后使用这个脚本:

$(document).ready(function() {
    $('._button').click(function(){
        $('.image').css('visibility', 'visible');
    });
});

这是jsfiddle:https://jsfiddle.net/cz5yyu83/1/

答案 1 :(得分:1)

使用jQuery:

$(document).ready(function() {
    $('._button').click(function() {
       $('.image').css('visibility','visible'); 
    });
});

https://jsfiddle.net/hyh9zajp/

使用纯Javascript,它也非常简单:

var button = document.getElementsByClassName('_button');
var images = document.getElementsByClassName('image');

button.addEventListener('click', function() {
    images.style.visibility = "visible";
});