[英]How to get the value of a {{variable}} of django template in javascript?
我現在從views.py
中獲取img 的值,我希望當用戶單擊圖像時,它應該以模式打開。
index.html
圖像部分:<!-- Card body -->
<div class="card-body">
{% for val in data.list %}
<img src="{{val.Img}}" class="img-thumbnail pop_img"> -------here i'm getting the image properly and the path is showing correctly
{% endfor %}
</div>
index.html
模態部分<div class="modal fade" id="imagemodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<img src="" class="imagepreview" style="width: 100%;" >
</div>
</div>
</div>
</div>
main.js
$(function() {
$('.pop_img').on('click', function() {
var img = $(this).find('img').attr('src')
console.log(img) -----------------but on console it prints : undefined
$('.imagepreview').attr('src', $(this).find('img').attr('src'));
$('#imagemodal').modal('show');
});
});
在檢查元素時,它會在模態中顯示<img src(unknown)>
。
如何獲取 src attr 值並將其放置在模態上?
該行的.find() function 返回 class 中的所有 img 元素, var img = $(this).find('img').attr('src')
。 這個問題可以通過使用 .first() 來解決。 所以var img = $(this).first('img').attr('src')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.