Changing Images src with Event “onclick”

前端 未结 5 1515
谎友^
谎友^ 2021-01-13 18:54








        
5条回答
  •  感动是毒
    2021-01-13 19:38

    try this code. just make sure to use (img.src.match) .And script inside the body.

    var img = document.getElementById("image1");
    img.addEventListener("mouseover", function(){
          if(img.src.match("images/image1.jpg")){
               img.src ="images/image1_2.jpg";}
           else {
               img.src = "images/image1.jpg"
                }  
    })
    

提交回复
热议问题