如何使用javascript在div上设置等宽

发布于 2024-09-04 02:10:19 字数 1059 浏览 3 评论 0原文

我有五行,如下所示: 这是一个带有 lis 的 ul。我希望所有行上的第一个 li“文本”都相同。为此,我需要寻找最宽的 li,然后将新的 li 应用于所有 li。

(ul > li > span & ul)

文本 >李1 |李2 |李3
文本长>李1 |李2 |李3
短>李1 |李2 |李3
更长13456>李1 |李2 | Li 3

我使用以下函数来设置相等的高度。稍微修改了一下。但这没有任何意义!

function equalWidth() {
    var span = $('#tableLookAlike li.tr > span'),
        tallest = 0, 
        thisHeight = 0;

    setEqWidth = function(elem) {
        elem.each(function() {
            widest = 0;
            thisWidth = $(this).outerWidth();
            console.log(tallest, thisWidth)



// if I remove this it gives me the right width
                elem.each(function() {Width)
                    if (thisWidth > widest) {
                        widest = thisWidth;
                        elem.css({'width': widest});
                    }
                });
            });
        }

        setEqWidth(span);
    }

当我删除第二个 elem.each 时,日志显示 92, 69, 68, 118,当我把它放回去时,它给我 92, 130, 168, 206。 有谁知道这里发生了什么事?

安威,我该如何解决这个问题?

I've got five rows that looks like this:
It's an ul with lis. I want the first li "text" to be equal on all rows. To do this I need to look for the widest li, then apply the new with to all li's.

(ul > li > span & ul)

Text > Li 1 | li 2 | Li 3
Textlong > Li 1 | li 2 | Li 3
Short > Li 1 | li 2 | Li 3
Longer13456 > Li 1 | li 2 | Li 3

I've used the following function for setting equal hights. Modified it a bit. But it does not make any sense!

function equalWidth() {
    var span = $('#tableLookAlike li.tr > span'),
        tallest = 0, 
        thisHeight = 0;

    setEqWidth = function(elem) {
        elem.each(function() {
            widest = 0;
            thisWidth = $(this).outerWidth();
            console.log(tallest, thisWidth)



// if I remove this it gives me the right width
                elem.each(function() {Width)
                    if (thisWidth > widest) {
                        widest = thisWidth;
                        elem.css({'width': widest});
                    }
                });
            });
        }

        setEqWidth(span);
    }

The log shows 92, 69, 68, 118 when I remove the second elem.each, and when I put it back, it gives me 92, 130, 168, 206.
Does anyone know whats going on here?

Aynway, how do I solve this?

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

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

发布评论

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

评论(5

多孤肩上扛 2024-09-11 02:10:19

如果我这么做的话,事情会是这样的:

var greatestWidth = 0;   // Stores the greatest width

$(selector).each(function() {    // Select the elements you're comparing

    var theWidth = $(this).width();   // Grab the current width

    if( theWidth > greatestWidth) {   // If theWidth > the greatestWidth so far,
        greatestWidth = theWidth;     //    set greatestWidth to theWidth
    }
});

$(selector).width(greatestWidth);     // Update the elements you were comparing

If I was to do it, it would go something like this:

var greatestWidth = 0;   // Stores the greatest width

$(selector).each(function() {    // Select the elements you're comparing

    var theWidth = $(this).width();   // Grab the current width

    if( theWidth > greatestWidth) {   // If theWidth > the greatestWidth so far,
        greatestWidth = theWidth;     //    set greatestWidth to theWidth
    }
});

$(selector).width(greatestWidth);     // Update the elements you were comparing
水晶透心 2024-09-11 02:10:19

更简洁一点

var widest = 0;
$("#tableLookAlike li.tr > span").each(function () { widest = Math.max(widest, $(this).outerWidth()); }).width(widest);

A bit more concise

var widest = 0;
$("#tableLookAlike li.tr > span").each(function () { widest = Math.max(widest, $(this).outerWidth()); }).width(widest);
彡翼 2024-09-11 02:10:19

您需要在循环末尾设置元素的宽度。否则,最宽值只是暂时的最高值。

setEqWidth = function(elem) {
    elem.each(function() {
        widest = 0;
        thisWidth = $(this).outerWidth();
        console.log(tallest, thisWidth)

        elem.each(function() {
                if (thisWidth > widest) {
                    widest = thisWidth;
                }
            });
        });


        elem.css({'width': widest});
    }

You need to set the width of the elements at the end of your loop. Otherwise the widest value is just a temporary highest value.

setEqWidth = function(elem) {
    elem.each(function() {
        widest = 0;
        thisWidth = $(this).outerWidth();
        console.log(tallest, thisWidth)

        elem.each(function() {
                if (thisWidth > widest) {
                    widest = thisWidth;
                }
            });
        });


        elem.css({'width': widest});
    }
伪心 2024-09-11 02:10:19

请记住:显式设置宽度不会考虑相关元素上的任何 padding,因此如果这些元素具有 padding-leftpadding-right set,由于 CSS 盒模型中的 padding 已合并到宽度中,您最终的宽度将大于原始最大宽度。

这甚至会使您的原始元素比开始时更宽。这同样适用于使用 padding-toppadding-bottom 设置高度。

如果您想要超级准确,则需要使用类似于下面的代码的内容来考虑 padding (如果您不使用百分比或 em 或像素分数来设置 padding,您可以使用 parseInt( ) 而不是 parseFloat( )):

var maxWidth = 0;
var $els = $('.your-selector');
$els.each(function(){
    var w = $(this).width();
    if(w > maxWidth){
        maxWidth = w;
    }

}

$els.each(function(){
    var $el = $(this);
    var padding = parseFloat($el.css('padding-left'),100) + parseFloat($el.css('padding-right'),100);

    $el.width(maxWidth - padding);
});

Remember: setting the width explicitly will not take into account any padding on the elements in question, so if those elements have padding-left or padding-right set, you're going to wind up with a width greater than your original maximum width due to padding being incorporated into width in the CSS box model.

This will even make your original element even wider than it started with. The same applies to setting height with padding-top and padding-bottom.

If you want to be super-accurate, you'll need to take padding into account with something like the code below (if you're not using percantages or ems or fractions of pixels to set padding, you can use parseInt( ) instead of parseFloat( )):

var maxWidth = 0;
var $els = $('.your-selector');
$els.each(function(){
    var w = $(this).width();
    if(w > maxWidth){
        maxWidth = w;
    }

}

$els.each(function(){
    var $el = $(this);
    var padding = parseFloat($el.css('padding-left'),100) + parseFloat($el.css('padding-right'),100);

    $el.width(maxWidth - padding);
});
只为一人 2024-09-11 02:10:19
function equalWidth() {
    var span = $('#tableLookAlike li.tr > span'),
        widest = 0,
        thisWidth = 0;

    setEqWidth = function(elem) {
        widest = 0;
        elem.each(function() {
            thisWidth = $(this).outerWidth();

            if (thisWidth > widest) {
                widest = thisWidth;
            }
        });

        elem.css({ 'width': widest });
    }
    setEqWidth(span);
}
function equalWidth() {
    var span = $('#tableLookAlike li.tr > span'),
        widest = 0,
        thisWidth = 0;

    setEqWidth = function(elem) {
        widest = 0;
        elem.each(function() {
            thisWidth = $(this).outerWidth();

            if (thisWidth > widest) {
                widest = thisWidth;
            }
        });

        elem.css({ 'width': widest });
    }
    setEqWidth(span);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文