jQuery - 获取 ul 内所有 li 的宽度

发布于 2024-12-20 07:26:01 字数 233 浏览 1 评论 0原文

我有一个ul。 ul 内有 x 个 li。里内有一个图像。

那就是:

<ul>
    <li>image</li>
    <li>image</li>
    <li>image</li>
</ul>

我希望能够获得ul的总宽度。这可能吗?

提前致谢。

I have a ul. Within the ul there are x number of li. Within the li there is a single image.

That is:

<ul>
    <li>image</li>
    <li>image</li>
    <li>image</li>
</ul>

I would like to be able to get the total width of the ul. Is this possible?

Thanks in advance.

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

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

发布评论

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

评论(3

乙白 2024-12-27 07:26:01

我会说

var internaWidth = $("ul").width();
// or
var widthIncludingPadding = $("ul").outerWidth();
// or
var widthIncludingMargin = $("ul").outerWidth(true);

取决于你需要什么

I'd say

var internaWidth = $("ul").width();
// or
var widthIncludingPadding = $("ul").outerWidth();
// or
var widthIncludingMargin = $("ul").outerWidth(true);

Depending on what you need

可是我不能没有你 2024-12-27 07:26:01

首先你应该给 ul 一个 id 然后这段代码可以解决问题:

<ul id="myul">
  <li>image</li>
  <li>image</li>
  <li>image</li>
</ul>

$(document).ready(function() {
   var totalWidth = $("#myul").width();
});

First of all you should give an id to ul then this code may solve the problem:

<ul id="myul">
  <li>image</li>
  <li>image</li>
  <li>image</li>
</ul>

$(document).ready(function() {
   var totalWidth = $("#myul").width();
});
三生一梦 2024-12-27 07:26:01
$("ul").width()

将为您提供 ul 元素的宽度。

$("ul").width()

Will get you the width of the ul element.

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