Programmatically change the src of an img tag

后端 未结 9 1665
执念已碎
执念已碎 2020-11-22 04:31

How can I change the src attribute of an img tag using javascript?


         


        
9条回答
  •  误落风尘
    2020-11-22 05:22

    You can use both jquery and javascript method: if you have two images for example:

    image
    image
    

    1)Jquery Method->

    $(".image2").attr("src","image1.jpg");
    

    2)Javascript Method->

    var image = document.getElementsByClassName("image2");
    image.src = "image1.jpg"
    

    For this type of issue jquery is the simple one to use.

提交回复
热议问题