jquery。如何获得兄弟姐妹的身高?

发布于 2024-09-10 06:56:43 字数 251 浏览 1 评论 0原文

我认为对于在 Jquery 方面更有经验的人来说这是一个非常简单的问题。

例如,我们有一个简单的 html 页面,其中有几个 div,其中 3 个具有相同的 css 类“sidebar”,

每个“sidebar”div 都有不同的内容和不同的高度。

我需要比较这个 div 的高度并找到最长的一个。

我知道如何实现比较,但我不知道如何在 Jquery 中将每个 div 存储

在 vairable 或数组中。

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 of this divs

to store their value in vairable or array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮光之海 2024-09-17 06:56:43

我不确定为什么到目前为止发布的两个答案都使用 jQuery.each()< /code>而不是仅仅调用 each()直接,所以这就是我的建议:

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

将每个高度放入数组中:

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

或者,只需使用 map ()

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

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();
喜爱皱眉﹌ 2024-09-17 06:56:43
$.each($('.sidebar'), function() {
    var height = $(this).height();
});
$.each($('.sidebar'), function() {
    var height = $(this).height();
});
魔法少女 2024-09-17 06:56:43

当我必须做同样的事情时,这是我使用的代码:

    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");
    });

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");
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文