Adding image using javascript

前端 未结 3 1476
情话喂你
情话喂你 2020-12-21 09:16

I have a html page in which there is an image in anchor tag code is :

         


        
3条回答
  •  遥遥无期
    2020-12-21 10:17

    As others have indicated, there are many ways to do this. The A element isn't an anchor, it's a link. And no one really uses XHTML on the web so get rid of the XML-style syntax.

    If you don't have an id for the image, then consider:

    function changeImage(id, src) {
    
      var image;
      var el = document.getElementById(id);
    
      if (el) {
        image = el.getElementsByTagName('img')[0];
    
        if (image) {
          image.src = src;
        }
      }
    }
    

    Then you can use an onload listener:

    
    

    or add a script element after the link (say just before the closing body tag) or use some other strategy for running the function after the image is in the document.

提交回复
热议问题