我试图获取图像的src,但是div是动态的,然后在页面上出现多次,当我点击DIV时,他总是获取第一帧而不是点击的DIV,怎么办我正确使用了 $(this)
吗?遵循我正在使用的代码:
<section class="clientes" role="region">
<a href="" title="Cliente" class="group">
<img src="img/image01.jpg" alt="" />
<h2>The title</h2>
<p>content here</p>
</a>
</section>
<script>
$("body").on("click", ".clientes", function(){
var pega = $(".clientes .dsp-none img").attr('src');
$(this).find("a").attr("href", pega);
})
最佳答案
利用 jQuery 安排 this
来引用单击的 DOM 元素:
var pega = $(this).find(".dsp-none img").attr('src');
您可能想也可能不想使用 .prop("src")
而不是 .attr()
。
关于javascript - 如何使用 $(this) 获取动态 DIV 中图像的 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809272/