简体   繁体   English

获取jquery / css / javascript中相同类元素的最小宽度

[英]Get the min width of the same class elements in jquery /css / javascript

<img class = "draggable moveable" id = "1" style="width:20px;" >...</img>
<img class = "draggable printable" id = "2" style="width:50px;" >...</img>
<img class = "draggable moveable" id = "3" style="width:10px;" >...</img>

For the case above, how to implement the jquery/css selector to get the minimum width of the same class?(which is 10px in this case) 对于上面的情况,如何实现jquery / css选择器以获得相同类的最小宽度?(在这种情况下为10px)

$('.draggable').('min:width');

are there such synatx? 有这样的synatx? thanks 谢谢

There is no such syntax, you can sort the element according to their width and use first method: 没有这样的语法,您可以根据宽度对元素进行排序并使用first一种方法:

$('.draggable').sort(function(a, b){
   return $(a).width() > $(b).width(); 
}).first();

http://jsbin.com/igovaw/1/edit http://jsbin.com/igovaw/1/edit

Note that img element doesn't have closing tag. 请注意, img元素没有结束标记。

If you want to get the minimum width and not the element you can use map method: 如果要获得最小宽度而不是元素,可以使用map方法:

var w = $('.draggable').map(function(){
   return $(this).width(); 
}).get();

var min = Math.min.apply(null, w);
var minwidth = undefined;
$('.draggable').each(function () {
    width = parseInt($(this).css('width'));
    if (minwidth === undefined || width < minwidth) {
        minwidth = width;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM