how to add a link to an image using jquery?

前端 未结 2 1484
既然无缘
既然无缘 2021-01-06 00:31
 
  • Catalogue
  • script:

    
    
            
    相关标签:
    2条回答
    • 2021-01-06 00:43

      img elements do not have href attributes. If you want the image to act as a link you have a couple of options. You could wrap the img in an a element:

      $("#pdf1img").wrap("<a href='/Content/pdf/" + data.pdf1 + "'>");
      

      Or you could bind a click event handler to the image and use window.location:

      $("#pdf1img").click(function() {
          window.location.href = "/Content/pdf/" + data.pdf1;
      });
      
      0 讨论(0)
    • 2021-01-06 00:59
      $("#pdf1img").wrap($('<a>',{
         href: '/Content/pdf/' + data.pdf1
      }));
      

      Try that ^^^

      Image elements cannot be links, but they can be wrapped in anchor elements which are.

      .wrap() in jQuery Docs

      0 讨论(0)
    提交回复
    热议问题