如何使用javascript在div上设置等宽
我有五行,如下所示: 这是一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我这么做的话,事情会是这样的:
If I was to do it, it would go something like this:
更简洁一点
A bit more concise
您需要在循环末尾设置元素的宽度。否则,最宽值只是暂时的最高值。
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.
请记住:显式设置宽度不会考虑相关元素上的任何
padding
,因此如果这些元素具有padding-left
或padding-right set,由于 CSS 盒模型中的 padding 已合并到宽度中,您最终的宽度将大于原始最大宽度。
这甚至会使您的原始元素比开始时更宽。这同样适用于使用
padding-top
和padding-bottom
设置高度。如果您想要超级准确,则需要使用类似于下面的代码的内容来考虑
padding
(如果您不使用百分比或 em 或像素分数来设置padding
,您可以使用parseInt( )
而不是parseFloat( )
):Remember: setting the width explicitly will not take into account any
padding
on the elements in question, so if those elements havepadding-left
orpadding-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
andpadding-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 setpadding
, you can useparseInt( )
instead ofparseFloat( )
):