chrome中使用jquery的图像高度问题

发布于 2024-08-10 23:54:15 字数 124 浏览 4 评论 0原文

$('img').height() 在 Chrome 中返回 0,但在 IE 和 Firefox 中返回实际高度。

在 Chrome 中获取图像高度的实际方法是什么?

$('img').height() returns 0 in chrome, but it returns the actual height in IE and firefox.

Whats the actual method to get the height of the image in chrome?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

◇流星雨 2024-08-17 23:54:15

正如 Josh 提到的,如果图像尚未完全加载,jQuery 将不知道尺寸是多少。尝试这样的操作,以确保您仅在图像完全加载后尝试访问其尺寸:

var img = new Image();

$(img).load( function() {
    //-- you can determine the height before adding to the DOM
    alert("height: " + img.height);
    //-- or you can add it first...
    $('body').append(img);
    //-- and then check
    alert($('body img').height());
}).error( function() {
    //-- this will fire if the URL is invalid, or some other loading error occurs
    alert("DANGER!... DANGER!...");
});

$(img).attr('src', "http://sstatic.net/so/img/logo.png");

As Josh mentioned, if the image has not fully loaded yet, jQuery won't know what the dimensions are yet. Try something like this to ensure you're only attempting to access it's dimensions after the image has been completely loaded:

var img = new Image();

$(img).load( function() {
    //-- you can determine the height before adding to the DOM
    alert("height: " + img.height);
    //-- or you can add it first...
    $('body').append(img);
    //-- and then check
    alert($('body img').height());
}).error( function() {
    //-- this will fire if the URL is invalid, or some other loading error occurs
    alert("DANGER!... DANGER!...");
});

$(img).attr('src', "http://sstatic.net/so/img/logo.png");
看轻我的陪伴 2024-08-17 23:54:15

我的假设是在图像加载完成之前调用该函数。您能否尝试延迟该函数以查看是否确实如此?如果是这种情况,您可以在运行该函数之前使用计时器进行作弊。

检测图像何时加载有点复杂。 看看这个脚本的一些想法 - 也许它会为你工作。

My assumption is that this function is being called before the image is finished loading. Can you try putting a delay on the function to see if this is actually the case? If this is the case, you can cheat by using a timer before running the function.

It is a bit more complicated to detect when an image has been loaded. Have a look at this script for some ideas -- maybe it would work for you.

守护在此方 2024-08-17 23:54:15

您可能需要预加载图像并设置回调函数:

// simple image preloader
function getHeight(el,fn) {
    var img = new Image();
    img.onload = function() { fn(img.height); };
    img.src = el.attr("src");
}

$("img").each(function(){
    getHeight($(this),function(h){
        // what you need to do with the height value here
        alert(h);
    });
});

Preloading the image and setting up a callback function might be what you're looking for:

// simple image preloader
function getHeight(el,fn) {
    var img = new Image();
    img.onload = function() { fn(img.height); };
    img.src = el.attr("src");
}

$("img").each(function(){
    getHeight($(this),function(h){
        // what you need to do with the height value here
        alert(h);
    });
});
公布 2024-08-17 23:54:15

我使用的一个稍微丑陋但有效的修复方法是使用后端语言(在我的例子中是 php)来获取高度并将其设置在图像上,然后再将其发送到浏览器。

它很丑陋,但是简单快捷

A slightly ugly but effective fix that I used was use a backend language, in my case php, to get the height and set it on the image before sending it to the browser.

it's ugly, but simple and quick

何其悲哀 2024-08-17 23:54:15

尝试使用图像预加载器,这是我写的获取另一个问题的答案。

Try using an image pre-loader, here's one I wrote for an answer to another question.

乄_柒ぐ汐 2024-08-17 23:54:15

刚刚做了一些实验,我发现

$(this).height();

返回 0 - 但是如果你只使用普通的旧 JS,

this.height;

我会得到正确的图像高度。

Having just done some experiments with this I've found that

$(this).height();

Returns 0 - however if you just use plain old JS

this.height;

I get the correct image height.

山色无中 2024-08-17 23:54:15

刚刚遇到了同样的问题:通过 CSS 或 HTML 预先设置宽度和高度。

Just had the same issue: set the width and height beforehand via CSS or HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文