简体   繁体   English

轻按链接时如何切换图像?

[英]How can a image be switched when a link is tapped?

The image should be changed as soon as the link is tapped. 点击链接后,应立即更改图像。 How can I realise this with javascript and html? 如何使用javascript和html实现此功能? I have tried and gathered information and come up with the code 我尝试并收集了信息并提出了代码

    <script type="text/javascript">
      function changeImage(element) {
      document.getElementById('imageReplaceTel').src = element;}
    </script>

<a href={{callphonenumber}} style="color:white" onclick="changeImage({{asset('bundles/testProject/images/telephoneSelected.png')}})">
    <img src="{{asset('bundles/testProject/images/telephone.png')}}" width="54px" height="54px" id="imageReplaceTel" alt="Images"></img></a>

I was hoping this would work but it failed, I might be missing something as I am a beginner with javascript. 我希望这可以工作,但是失败了,因为我是javascript的初学者,所以可能会缺少一些东西。 Thanks 谢谢

using javascript and html it would look like this 使用javascript和html看起来像这样

 <script type="text/jscript"> function Changetheimage() { document.getElementById("img1").src = "imagefoldername/Imagename2.jpg"; } </script> </head> <body> <a onclick="Changetheimage()">click here</a> <img src="imagefoldername/Imagename1.jpg" id="img1" alt="" /> 

Notes: "Img1" must be replaced with the id of the image you want to change "imagefoldername/Imagename2.jpg" in the javascript must be changed to the directory of the new image you want to display and "imagefoldername/Imagename1.jpg" must be changed to the image currently displaying 注意:“ Img1”必须替换为要更改的图像的ID,而javascript中的“ imagefoldername / Imagename2.jpg”必须更改为要显示的新图像的目录,以及“ imagefoldername / Imagename1.jpg”必须更改为当前显示的图像

最好的方法是使用图像sprite.onclick只需通过javascript更改sprite坐标即可。

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

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