首页 > 解决方案 > 如何在图像 src 标签上放置文本

问题描述

我的代码有问题(我还在学习 js)。我想把图片链接到图片src标签,我不知道怎么做,你能帮我吗?

这是我的代码:

$(document).ready(function(){
    $(".img").click(function(){
        $("#img_link").text($(this).attr('src'));
        $('#myModal').modal('hide') ;
    });
});

<button type="button" class="btn btn-primary hd" data-toggle="modal" data-target="#myModal">add img</button>
<img id="img_link" src=""/>

标签: javascriptjquery

解决方案


$(document).ready(function(){
$(".btn").click(function(){
$("#img").attr("src", "https://i.pinimg.com/736x/ab/b6/a8/abb6a800ab2193fcedd9bda566b7402c.jpg");
$("#img_link").text($("#img").attr('src'));
 //$('#myModal').modal('hide') ;
});
});
<div class="img_content">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <button type="button" class="btn btn-primary hd" data-toggle="modal" data-    target="#myModal">add img</button> 
  <p id="img_link"></p>
  <img id="img" src=""/>
</div>


推荐阅读