Show/hide image with JavaScript

后端 未结 6 642
抹茶落季
抹茶落季 2020-12-08 15:17

I have an HTML page with an image that I set to be invisible by CSS visibility: hidden. I want to make a link called \"Show image\", so that when I click on it,

相关标签:
6条回答
  • 2020-12-08 15:24

    This is working code:

    <html>
      <body bgcolor=cyan>
        <img src ="backgr1.JPG" id="my" width="310" height="392" style="position: absolute; top:92px; left:375px; visibility:hidden"/>
        <script type="text/javascript">
          function tend() {
            document.getElementById('my').style.visibility='visible';
          }
          function tn() {
            document.getElementById('my').style.visibility='hidden';
          }
        </script>
        <input type="button" onclick="tend()" value="back">
        <input type="button" onclick="tn()" value="close">
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-08 15:27

    If you already have a JavaScript function called showImage defined to show the image, you can link as such:

    <a href="javascript:showImage()">show image</a>
    

    If you need help defining the function, I would try:

    function showImage() {
        var img = document.getElementById('myImageId');
        img.style.visibility = 'visible';
    }
    

    Or, better yet,

    function setImageVisible(id, visible) {
        var img = document.getElementById(id);
        img.style.visibility = (visible ? 'visible' : 'hidden');
    }
    

    Then, your links would be:

    <a href="javascript:setImageVisible('myImageId', true)">show image</a>
    <a href="javascript:setImageVisible('myImageId', false)">hide image</a>
    
    0 讨论(0)
  • 2020-12-08 15:27

    It's pretty simple.

    HTML:

    <img id="theImage" src="yourImage.png">
    <a id="showImage">Show image</a>
    

    JavaScript:

    document.getElementById("showImage").onclick = function() {
        document.getElementById("theImage").style.visibility = "visible";
    }
    

    CSS:

    #theImage { visibility: hidden; }
    
    0 讨论(0)
  • 2020-12-08 15:31

    Here is a working example: http://jsfiddle.net/rVBzt/ (using jQuery)

    <img id="tiger" src="https://twimg0-a.akamaihd.net/profile_images/2642324404/46d743534606515238a9a12cfb4b264a.jpeg">
    
    <a id="toggle">click to toggle</a>
    
    img {display: none;}
    
    a {cursor: pointer; color: blue;}
    
    $('#toggle').click(function() {
        $('#tiger').toggle();
    });
    
    0 讨论(0)
  • 2020-12-08 15:42

    You can do this with jquery just visit http://jquery.com/ to get the link then do something like this

    <a id="show_image">Show Image</a>
    <img id="my_images" style="display:none" src="http://myimages.com/img.png">
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
       $(document).ready(function(){
          $('#show_image').on("click", function(){
             $('#my_images').show('slow');
          });
       });
    </script>
    

    or if you would like the link to turn the image on and off do this

    <a id="show_image">Show Image</a>
    <img id="my_images" style="display:none;" src="http://myimages.com/img.png">
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
       $(document).ready(function(){
          $('#show_image').on("click", function(){
             $('#my_images').toggle();
          });
       });
    </script>
    
    0 讨论(0)
  • 2020-12-08 15:42

    HTML

    <img id="theImage" src="yourImage.png">
    <a id="showImage">Show image</a>
    

    JavaScript:

    document.getElementById("showImage").onclick = function() {
        document.getElementById("theImage").style.display = "block";
    }
    

    CSS:

    #theImage { display:none; }
    
    0 讨论(0)
提交回复
热议问题