简体   繁体   English

javascript参数参考错误

[英]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 ? 我试图制作一个照片库,用户可以单击链接并显示不同的照片,但是试图将参数作为名称输入,但是它一直在显示错误,但仍表示该参考错误,但是当我尝试使用像0,1,2这样的数字时,3,4,5一切正常,就像我想要的一样。我的问题是如何使参数成为名称而不是数字?

  <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 . gusionselena是名称……具体地说,它们是变量名

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>

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

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