我的代码有问题(我仍在学习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=""/>
答案 0 :(得分:0)
$(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>
答案 1 :(得分:0)
您必须使用,
$("#img_link").attr('src', 'the text you want to go into src')
由于您没有提供所有代码,所以我认为文字应该是
$(this).text()