6
<div id="c1" class="features" style="height:100px;width:100px;"></div>
<div id="c2" class="features" style="height:120px;width:100px;"></div>
<div id="c3" class="features" style="height:90px;width:100px;"></div>
<div...> 

JQueryを使用して最短を見つけるにはどうすればよいdivですか?

たとえば、高さが 90px であるため、上記の結果は div id="c3" になります。

4

5 に答える 5

11
var shortest = [].reduce.call($(".features"), function(sml, cur) {
    return $(sml).height() < $(cur).height() ? sml : cur;
});
于 2012-11-10T03:42:45.217 に答える
0

JQueryを使わずにそれを行う方法は次のとおりです。

var allDivs = document.getElementsByTagName("div");

var min = 0;
var element;

for (var i = 0; i < allDivs.length; i++) {
    if (allDivs[i].className == "features") {
        if (allDivs[i].offsetHeight < min) {
            min = allDivs[i].offsetHeight;
            element = allDivs[i];
        }
    }
}

alert(element);
于 2012-11-10T03:32:16.887 に答える
0

要素の計算された高さの値を返すjQuery.height()メソッドを使用します。

var candidates = getSetOfDivs();
var smallest = candidates[0];
$( candidates ).each( function(e, i) {
    if( $(e).height() > smallest.height() ) smallest = $(e);
} );
于 2012-11-10T03:32:27.550 に答える
0

あなたは本当に最小のものを望んでいましたか?それとも、身長が最も低いものが欲しかったのですか?それらは別のものです。これは、面積で最小のものを見つけるソリューションです(ただし、に置き換えることで高さで移動できます$(this).height() * $(this).width()$(this).height()

var divs = $('.features');

var smallest;
var smallestarea = null;

$('.features').each(function () {
  var thisarea = $(this).height() * $(this).width();
  if (smallestarea === null || thisarea < smallestarea) {
    smallest = $(this);
    smallestarea = thisarea;
  }
});

document.write(smallest.attr('id'));

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

于 2012-11-10T03:32:48.943 に答える