如何从我在该函数内的 jQuery 函数中创建的 img 中查找/选择图像高度?

发布于 2024-11-29 11:30:23 字数 1301 浏览 1 评论 0原文

$("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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

背叛残局 2024-12-06 11:30:23

您需要等待图像加载才能获取其高度:

$("#galleryImg").html("<img src='images/full_size/" + selectedImg + "' alt='" + selectedImg + "' />");
$("#galleryImg img").load(function() {
    var imgHeight = $(this).height();
});

You'll need to wait for the image to load before getting its height:

$("#galleryImg").html("<img src='images/full_size/" + selectedImg + "' alt='" + selectedImg + "' />");
$("#galleryImg img").load(function() {
    var imgHeight = $(this).height();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文