jquery - 获取图像的原始宽度和高度

标签 jquery css image

我有一张图片,假设它由一个变量引用 var IMG = $("img") 我想捕捉它的原始高度和宽度。我正在使用下面的代码。它运行良好,但在 Win XP IE8 中不起作用。谁能告诉我问题出在哪里?

$("<img/>")
    .attr("src", IMG.attr("src"))
    .load(function() {
        //Execution is not coming in this block
        alert(this.width);

    });

最佳答案

在所有情况下,您都应该在 onload 处理程序之后设置 src,尤其是对于缓存图像,尝试看看是否有所不同:

$("<img/>")    
    .load(function() {
        //Execution is not coming in this block
        alert(this.width);

    })
    .attr("src", IMG.attr("src"));

关于jquery - 获取图像的原始宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542244/

相关文章:

html - 文本环绕绝对定位的 div

html - 图像适合特定尺寸的 flexbox

javascript - 图像尺寸和文件大小之间有关系吗?

javascript - $.ajax 从 php 文件取回数据时出错

javascript - 样式导航栏的方式类似于一个附加的

javascript - Twitter-Bootstrap 表单验证样式

javascript - jQuery 元素位置在显示和停止动画后重置

html - 浏览器调整大小时,下拉菜单不会保持居中

android - 旋转图像时如何避免 OutOfMemory ex?

jquery - 处理我希望用户不可见的 DOM 元素