简体   繁体   中英

javascript parameters reference errors

i was trying to make a photo gallery where user can click the link and show different photo but itry to put the parameters as a name but it keeps getting error says that said reference error but when i try to use number like 0,1,2,3,4,5 everything goes normal exactly like i want.My question is how to make the parameters to be a name instead of a number ?

  <body>
<img src="../bab2/images/Gusion.png" width="400px" alt="" id="gambar">
<a href="#" onclick="ganti(gusion)">1</a>
<a href="#" onclick="ganti(selena)">2</a>
<a href="#" onclick="ganti(2)">3</a>
<a href="#" onclick="ganti(3)">4</a>
<a href="#" onclick="ganti(4)">5</a>
<script type="text/javascript">
  function ganti(nama){
    var lokasi = '../bab2/images/' + nama + '.png';
    document.getElementById('gambar').src =lokasi;
  }
</script>

gusion and selena are names… specifically, they are variable names .

If you want a string then you need to surround it with quotes .

If you want to pass names as parameters you need to pass them as Strings, ie, enter the name within quotes. Here is the modified code:

<a href="#" onclick="ganti('gusion')">1</a>
<a href="#" onclick="ganti('selena')">2</a>

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