简体   繁体   中英

How to load an image on button click?

I'm kind of new to HTML / Javascript, and I'm trying to figure out how to make it so when I click a button, an image loads. This is all I can come up with, but then again I am very bad with this stuff, and I am still learning greatly .

<html>
<div style="visibility:invisible" id="theTrick">
<img id="myImage" src="https://minecraft.net/images/logo.png" alt="Test">
</div>
<button type="button" onclick="myFunction">Click for the Minecraft Logo</button>
<script>
function myFunction() {

    document.getElementById="theTrick".style = "visibility:initial"

}
</script>
</html>

EDIT

I have solved this, and since then learned a LOT more, mastering HTML and CSS, and now learning the works of JavaScript. I do have a question though to add on to this; is there a way to have the image disappear on another click, then reappear on the next click, and so on?

A few things:

  1. It's visibility: hidden , not visibility: invisible .
  2. It's myFunction() , not myFunction .
  3. It's document.getElementById("theTrick") , not document.getElementById = "theTrick" .
  4. It's visibility: visible , not visibility: initial (the initial visibility is hidden , which you've set at the top).

You may find this document on the CSS visibility property useful.

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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