我可以使用Jquery自动查找和设置各种图像的宽度和高度吗? [英] Can I use Jquery to automatically find and set the width and height of a variety of images?

查看:68
本文介绍了我可以使用Jquery自动查找和设置各种图像的宽度和高度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Masonry插件,为了使其能够正确处理图像,您需要在image标签中设置宽度和高度.所有图像的高度都会变化,但是宽度将设置为200px.

I am using the Masonry Plugin and for it to work correctly with images, you need to set the width and height within the image tag. All the image's heights will vary, but the width will be set at 200px.

Jquery是否可以检测每个图像的高度并设置其高度?这样,我不必设置每个图像的高度.

Is there a way for Jquery to detect the height of each image and set it's height? That way I don't have to go set the height of every single image.

这有意义吗?

希望有人可以提供帮助!我是Jquery新手.

Hope someone can help! I am new to Jquery.

推荐答案

是的,您可以使用jQuery自定义选择器,如下所示:

Yes there is, you can use jquery custom selectors like this:

//Add custom selector that returns all objects taller than 200px
//See http://www.bennadel.com/blog/1457-How-To-Build-A-Custom-jQuery-Selector.htm
$.extend($.expr[':'], {
    over200pixels: function(a) {
        return $(a).height() > 200;
    }
});

//  Seclect all images taller than 200px
//  Set height to 200px using .css() method 
//  See http://api.jquery.com/css/ 
$('img:over200pixels').css("height","200px");

应归功于其功劳的功劳: jQuery技巧和窍门

Credit where credit is due: jQuery Tips and Tricks

在线演示: http://jsfiddle.net/hvq5m/

这篇关于我可以使用Jquery自动查找和设置各种图像的宽度和高度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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