<a href="http://bbs.csdn.net" class="action">示例</a><img src="/a/pro.png" />
现在这个图片是没有带链接的,然后我想用jquery来给图片加上面action的链接。这个用jquery要怎么写?谢谢。!

解决方案 »

  1.   

    先给img加个ID myimg
    然后$("#myimg").parent().html("<a href='http://bbs.csdn.net' class='action'><img id='myimg' src='/a/pro.png' /></a>")
      

  2.   

    自动遍历div内所有img标签对象加上指定的host可以这样写
    <script
      src="https://code.jquery.com/jquery-3.4.1.js"
      integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
      crossorigin="anonymous">
    </script>
    <a href="http://bbs.csdn.net" class="action">示例</a>
    <div class="frame">
    <img src="/a/pro.png" />
    <img src="/a/pro2.png" />
    </div>
    <script>
    var host = $(".action").attr("href");
    $("div[class=frame] img").each(function() {   
            $(this).attr("src",host + $(this).attr("src"));
    });
     </script>