HTML:
<div class="modal fade" data-backdrop="static" role="dialog">
<div class="modal-dialog" role="document">
<img/>
</div>
</div>
JS:
$(".btn").click(function() {
$(".modal img").prop("src", "new src");
alert($(".modal img").height());
$(".modal").modal("show");
});
提醒:0
您好,我正在尝试在更改src后获取图像高度。
我在引导模式中有一个图像,单击按钮更改图像的src并显示模态。
我无法获得正确的图像高度,因为图像不会加载src直到模态可见,所以任何方式强制src加载,或任何方式来获得正确的高度.BTW我试过onload
和{{ 1}},都不起作用。
答案 0 :(得分:0)
试试这个:https://jsfiddle.net/yyma7fkj/
$(".btn").click(function() {
$(".modal img").load(function() {
alert("height:" + this.height + " width:" + this.width);
$(".modal").modal("show");
}).prop("src", "http://jsfiddle.net/img/logo.png");
});