如何从我在该函数内的 jQuery 函数中创建的 img 中查找/选择图像高度?
$("img").click(function () {
$("#galleryImg").html("<img src='images/full_size/" + selectedImg + "' alt='" + selectedImg + "' />");
var imgHeight = $("#galleryImg img").height();
}
我现在使用的方法不起作用,我无法使用 $(this) 因为我想要的高度的图像与我单击的图像不同。更复杂的是,我想要的图像的高度是在函数中创建的 html img 标签。
相关区域的整段代码如下
$("img").click(function () {
$("div#blackOut").fadeTo(550, 0.8);
$("div#whiteBox").fadeTo(550, 1);
var browserHeight = $(window).height();
var browserWidth = $(window).width();
var imgName = $(this).attr("src");
var imgArray = $(this).attr('src').split('\/');
var selectedImg = imgArray[imgArray.length-1];
$("#galleryImg").html("<img src='images/full_size/" + selectedImg + "' alt='" + selectedImg + "' />");
$("#galleryImg img").load(function() {
var thatHeight = $("#galleryImg img").height();
alert(thatHeight);
});
var top = (browserHeight - (imgHeight + 50)) / 2;
var width = imgHeight + 50;
var marginLeft = -(width / 2);
var top = top + "px";
var width = width + "px";
var marginLeft = marginLeft + "px";
$("#whiteBox").css("top", top);
$("#whiteBox").css("width", "width");
$("#whiteBox").css("margin-left", "marginLeft");
});
$("img").click(function () {
$("#galleryImg").html("<img src='images/full_size/" + selectedImg + "' alt='" + selectedImg + "' />");
var imgHeight = $("#galleryImg img").height();
}
The method I'm using right now is not working and I can't use $(this) because the image that I want the height from is different from the image I am clicking on. And to complicate it a little bit more, the image I want the height from is having its html img tag being created within the function.
The whole piece of code of the relevant area is as below
$("img").click(function () {
$("div#blackOut").fadeTo(550, 0.8);
$("div#whiteBox").fadeTo(550, 1);
var browserHeight = $(window).height();
var browserWidth = $(window).width();
var imgName = $(this).attr("src");
var imgArray = $(this).attr('src').split('\/');
var selectedImg = imgArray[imgArray.length-1];
$("#galleryImg").html("<img src='images/full_size/" + selectedImg + "' alt='" + selectedImg + "' />");
$("#galleryImg img").load(function() {
var thatHeight = $("#galleryImg img").height();
alert(thatHeight);
});
var top = (browserHeight - (imgHeight + 50)) / 2;
var width = imgHeight + 50;
var marginLeft = -(width / 2);
var top = top + "px";
var width = width + "px";
var marginLeft = marginLeft + "px";
$("#whiteBox").css("top", top);
$("#whiteBox").css("width", "width");
$("#whiteBox").css("margin-left", "marginLeft");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要等待图像加载才能获取其高度:
You'll need to wait for the image to load before getting its height: