nas*_*son 6 html javascript jquery offsetheight
<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...>
Run Code Online (Sandbox Code Playgroud)
我如何使用JQuery找到最短的div?
例如,上面会导致div id ="c3",因为它的高度是90px.
I H*_*azy 10
var shortest = [].reduce.call($(".features"), function(sml, cur) {
return $(sml).height() < $(cur).height() ? sml : cur;
});
Run Code Online (Sandbox Code Playgroud)
以下是不使用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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2589 次 |
| 最近记录: |