如何使用 jquery 获取最长宽度作为所有 $('li') 的宽度
<ul>
<li>Short</li>
<li>Short</li>
<li>Short
<ul>
<li>Short</li>
<li>Longer</li>
<li>This is the longest (how do i get this longest width as width of all LI siblings with in this UL only)</li>
<ul>
</li>
</ul>
<ul>
<li>Short</li>
<li>Short</li>
<li>Short
<ul>
<li>Short</li>
<li>Longer</li>
<li>This is the longest (how do i get this longest width as width of all LI siblings with in this UL only)</li>
<ul>
</li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许您可以使用
offsetWidth
属性。<代码>
If you want to return the 'li'element with the longest width, you should probably save the 'i' indice in the function and then you could select it such as:
$('li:nth-child('+i+')')
.The function would then be :
Maybe you could use the
offsetWidth
propriety.If you want to return the 'li'element with the longest width, you should probably save the 'i' indice in the function and then you could select it such as:
$('li:nth-child('+i+')')
.The function would then be :
ul 应该环绕 li 并成为最宽 li 的宽度,因此查看 ul 的宽度应该可以解决问题,除非 ul 是 display: block 和 non-floatet
The ul should wrap around the li's and become the width of the widest li, so looking at the width of the ul should do the trick, unless the ul is display: block and non-floatet
你实际上不能这样做,因为
li
元素是块级的。这意味着它们会自动占据父元素的整个宽度。 请参阅此 jsFiddle 来说明这一点。You can't really do this, because
li
elements are block level. This means that they automatically take up the entire width of the parent element. See this jsFiddle to show this.li-tag 的宽度取决于 ul-width,因此如果您有 ul display:block,li-width 将返回 ul 的宽度(100%);
但是您可以获取 li 标签内内联元素的宽度。
如果 li 内没有内联元素:-)
The width of the li-tag depends on the ul-width, so if you have the ul display:block the li-width will return the width of the ul (100%);
BUT you can get the the width of a inline-element inside a li-tag.
if you don't have a inline-element inside the li :-)