Change image source with JavaScript

后端 未结 13 1812
我寻月下人不归
我寻月下人不归 2020-11-27 18:20

So I\'m new with JavaScript (this is actually my first attempt to make something work) and I\'m having a bit of trouble. I thought I had enough knowledge to make this work,

相关标签:
13条回答
  • 2020-11-27 19:17

    You've got a few changes (this assumes you indeed still want to change the image with an ID of IMG, if not use Shadow Wizard's solution).

    Remove a.src and replace with a:

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

    Change your onclick attributes to include a string of the new image source instead of a literal:

    onclick='changeImage( "1772031_29_b.jpg" );'
    
    0 讨论(0)
提交回复
热议问题