简体   繁体   English

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

[英]jQuery - get width of all li within a ul

I have a ul. 我有病。 Within the ul there are x number of li. ul中有x个li。 Within the li there is a single image. 在li内只有一个图像。

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. 我希望能够获得ul的总宽度。 Is this possible? 这可能吗?

Thanks in advance. 提前致谢。

I'd say 我会说

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

Depending on what you need 根据您的需要

First of all you should give an id to ul then this code may solve the problem: 首先,您应该给ul指定一个ID,然后此代码可以解决问题:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM