开发者

jquery. how to get each siblings height?

开发者 https://www.devze.com 2023-01-07 04:35 出处:网络
I think this is very easy question for someone who is more experienced in Jquery. for example we havesimple html page with a few divs, and 3 of them have the same css class \'sidebar\'

I think this is very easy question for someone who is more experienced in Jquery.

for example we have simple html page with a few divs, and 3 of them have the same css class 'sidebar'

Each of this 'sidebar' divs have different content and different height.

I need to compare this divs height and find the longest one.

I know how to realize comparing, but I do not know how in Jquery I can take each开发者_Go百科 of this divs

to store their value in vairable or array.


I'm not sure why the two answers posted so far are using jQuery.each() rather than just calling each() directly, so here's what I would recommend:

$('#elementID').siblings().each(function ()
{
    var height = $(this).height();
});

To put each height into an array:

var heights = [];
$('#elementID').siblings().each(function ()
{
   heights.push($(this).height());
});

Or, just use map():

var heights = $('#elementID').siblings().map(function ()
{
   return $(this).height();
}).get();


$.each($('.sidebar'), function() {
    var height = $(this).height();
});


When I had to do the same thing, this is the code I used:

    var tabPanels = $("...");
    var maxHeight = 0;

    $.each(tabPanels, function() {
        var height = $(this).height();
        maxHeight = (height > maxHeight) ? height: maxHeight;
    });

    $.each(tabPanels, function() {
        $(this).css("height", maxHeight + "px");
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消