jQuery:添加到 DOM 的新图像加载后宽度始终为 0
我需要使用动态创建的图像标签来获取图像的尺寸。有用。但仅使用 attr('width') 并且仅当我不将图像添加到 DOM 时。否则,返回的尺寸为零。为什么? :)
this.img = $('<img/>', {'src': e.url} ); // generate image
this.img.appendTo('body'); // add to DOM
this.img.load(function(self){return function(){ // when loaded call function
console.log(self.img.attr('src'));
console.log(self.img.attr('width'));
console.log(self.img.width());
console.log(self.img.css('width'));
}; }(this) ); // pass object via closure
因此,我生成图像,将其添加到 DOM 并定义一个在加载图像时调用的处理函数。我使用闭包向其传递对原始对象的引用,在闭包中称为“self”。我正在转储图像的 URL 以确保引用正确。输出是:
pic.jpg
0
0
0px
现在这是奇怪的事情:如果我删除上面的第二行(appendTo),那么它突然起作用,但仅适用于 attr('width'):
pic.jpg
1024
0
0px
这种行为不会使对我来说有任何意义。我希望获得上述所有六种情况下的实际图像大小。我现在知道如何解决该问题,但我想了解为什么会发生这种情况。这是一个错误吗?或者有什么逻辑上的原因吗?
以上结果适用于 Safari。我刚刚使用 Chrome 进行了测试,在第一个示例中得到了所有正确的值,但在第二个示例中仍然有两个零。很奇怪。
I need to obtain the dimensions of an image, using a dynamically created image tag. It works. But ONLY using attr('width') and ONLY if I do NOT add the image to the DOM. Otherwise, dimensions returned are zero. Why? :)
this.img = $('<img/>', {'src': e.url} ); // generate image
this.img.appendTo('body'); // add to DOM
this.img.load(function(self){return function(){ // when loaded call function
console.log(self.img.attr('src'));
console.log(self.img.attr('width'));
console.log(self.img.width());
console.log(self.img.css('width'));
}; }(this) ); // pass object via closure
So I generate the image, add it to the DOM and define a handler function to be called when the image is loaded. I'm using a closure to pass it a reference to the original object, called "self" within the closure. I'm dumping the image's URL to make sure the reference is alright. Output is:
pic.jpg
0
0
0px
Now here is the strange thing: if I remove the second line above (the appendTo), then it suddenly works, but only for attr('width'):
pic.jpg
1024
0
0px
This behavior doesn't make any sense to me. I would expect to get the actual image size in all six above cases. I know now how to work-around the problem, but I'd like to understand why it happens. Is it a bug? Or is there some logical reason for it?
The above results are for Safari. I just tested with Chrome and I got all correct values in the first example, but still two zeros in the second example. Very weird.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了一个可靠的 Safari 解决方案:
我仍然不明白为什么宽度应该返回零,但如果仅在设置 src 之后附加负载处理程序,似乎会发生意外的事情。我之所以有这个想法,只是因为我遇到了 IE 的另一个问题(它甚至没有调用加载处理程序,因为它认为在附加处理程序之前加载了图像),所以我学到的教训是始终在上述顺序。
I found a reliable solution for Safari:
src
attribute!!I still don't understand why width should ever return zero, but it seems that unexpected things happen if you attach the load handler only after setting the src. I got that idea only because I encountered yet another problem with IE (which didn't even call the load handler since it considered the image loaded before the handler was attached), so the lesson I've learnt is to always do things in the order described above.
在 firebug 和 chrome 中尝试了您的代码,它的行为符合预期:
得到以下结果:
删除
appendTo()
行后,我能够在 Chrome 中重现该行为,但我不觉得奇怪。width()
正在计算“显示”元素的实际宽度 - 例如,每次将图像放入隐藏的 div 中时,它都会 == 0 !尝试用以下内容替换appendTo行,您将得到相同的结果:
this.img.appendTo( $('
更新:
当附加到 DOM 时,Safari 3.1.2 的结果对我来说与 Chrome 完全相同。
当 img 没有插入到 DOM 中时,我也得到了可接受的(没有奇怪的)结果:
我相信你的设置一定有一些独特的东西......或者它是一个 Safari @ Mac OSX 错误(在 Windows 环境下尝试过 Safari) )。
tried Your code in firebug and chrome and it behaves as expected :
got the following :
after removing the
appendTo()
line I'm abble to reproduce the behaviour in Chrome, but I do not find it weird.width()
is calculating the actual width for the "displayed" element - e.g. everytime You put the image in a hidden div it will be == 0 !try replacing the appendTo line with the following and You'll get the same :
this.img.appendTo( $('<div/>').css('display', 'none') );
UPDATE:
Results from Safari 3.1.2 are acting exactly the same for me as Chrome when appended to DOM.
When img is not inserted into the DOM I'm also getting acceptable (no weirdness) results :
I'm convinced there must be something unique to Your setup ... or it is a Safari @ Mac OSX bug (tried Safari on Windows env).
在我看来,分配 jQuery 对象然后在图像上调用加载函数的方式有点过头了,这使得很难理解为什么 jQuery 会出现这样的行为。
试试这个:
非常适合我在将图像注入 DOM 后获取它。
你也可以尝试这个:
It seems to me that the way you are assigning the jQuery object and then calling the load function on the image is a bit of an over kill, which makes it hard to see why jQuery might behave thus.
Try this:
Works perfectly for me to just get it after the image is injected into the DOM.
You could also try this:
我认为你的关闭没有按你想要的方式工作。你有没有尝试过类似的事情:
I don't think your closure is working the way you want. Have you tried something like: