Programmatically change the src of an img tag

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

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


         


        
9条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 05:29

    In this case, as you want to change the src of the first value of your element, you have no need to build up a function. You can change this right in the element:

    
      
    
    

    You have several ways to do this. You can also create a function to automatize the process:

    function changeSrc(p, t) { /* where p: Parent, t: ToSource */
      p.firstChild.src = t
    }
    

    Then you can:

    
      
    
    

提交回复
热议问题