Javascript simple onclick image swap

前端 未结 6 1785
终归单人心
终归单人心 2020-12-19 18:47

I am trying to use Javascript to swap an image, so far I can get it from A to B, but not back.

Here is what I\'m using to create one swap:



        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 19:39

    1. window.document.pic.src='pic1.png' assigns pic1.png to the left hand side. It does NOT compare.

    2. Though not directly relevant, try not to access elements by their name globally. Use their id.

    3. Your javascript should not be inside the onclick. It should be inside a javasctipt function

    Combined:

    The img tag:

    
    

    The javascript

    
    

提交回复
热议问题