jQuery的最小/从元素的数组最大财产 [英] jQuery min/max property from array of elements

查看:117
本文介绍了jQuery的最小/从元素的数组最大财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法来找到元素的jQuery的数组?

Is there a simple way to find the min/max property from an array of elements in jQuery?

我不断发现自己基于所述最小和最大的对应动态调整元素组。大多数时候,这涉及到一个元素的宽度和/或高度,但我敢肯定,这可以应用到一个元素的任何属性。

I constantly find myself dynamically resizing groups of elements based on the minimum and maximum counterparts. Most of the time this pertains to the width and/or height of an element but I'm sure this could be applied to any property of an element.

我平时做这样的事情:

var maxWidth = 0;

$('img').each(function(index){
if ($(this).width() > maxWidth)
{
maxWidth = $(this).width();
}
});

但好像你应该​​能够做这样的事:

But it seems like you should be able to do something like this:

var maxWidth = $('img').max('width');

这是否存在功能jQuery中或有人可以解释如何创建,这是否一个基本的插件吗?

Does this functionality exist in jQuery or can someone explain how to create a basic plugin that does this?

谢谢!

推荐答案

使用快速的JavaScript最大值/最小值 - 约翰Resig的

与谷歌,雅虎和Bing三个标志为例。

Example with three logos of google, yahoo and bing.

HTML

<img src="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png" alt="Google Logo" /><br/>
<img src="http://l.yimg.com/a/i/ww/met/yahoo_logo_in_061509.png" alt="Yahoo Logo" /><br/>
<img src="http://www.bing.com/fd/s/a/h1.png" alt="Bing Logo" />

的JavaScript

Javascript

$(document).ready(function(){
    // Function to get the Max value in Array
    Array.max = function( array ){
        return Math.max.apply( Math, array );
    };

    // Function to get the Min value in Array
    Array.min = function( array ){
       return Math.min.apply( Math, array );
    };

    //updated as per Sime Vidas comment.
    var widths= $('img').map(function() {
        return $(this).width();
    }).get();

    alert("Max Width: " + Array.max(widths));
    alert("Min Width: " + Array.min(widths));
});

P.S:的jsfiddle这里

这篇关于jQuery的最小/从元素的数组最大财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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