JQuery在图像上加载事件 [英] JQuery load event on images

查看:141
本文介绍了JQuery在图像上加载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在加载图像时将图像父级的大小调整为相同大小的图像。

I want to resize an image parent to the same size of the image, when the images are loaded.

此时我正在使用此代码:

At this time i'm using this code:

$(window).load(function(){
    $('.image-principale').each(function(){
        $(this).parent().css('height', $(this).height());
    });
});

除了仅在每个图像加载后才运行,它才有效。
我试图直接为每个图像添加一个加载处理程序,但它们不会触发。

It work, except than it runs only when every image has loaded. I tried to add an load handler to every image directly but they doesn't trigger.

出了什么问题?

谢谢!

推荐答案

请尝试以下方法:

...Your HTML...

<script type="text/javascript">
    $('.image-principale').load(function(){
        $(this).parent().css('height', $(this).height());
    });
</script>

请注意,脚本必须放在HTML之后,否则它将在<$ c $之前运行c>< img> 元素存在且实际上不会做任何事情。

Note that the script must be placed after the HTML, or it will run before the <img> elements exist and will not actually do anything.

您也可以尝试使用 live ,像这样:

You could also try using live, like this:

$('.image-principale').live('load', function(){
    $(this).parent().css('height', $(this).height());
});

但是,我不知道它是否真的有效。

However, I don't know whether it will actually work.

这篇关于JQuery在图像上加载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆