javascript - 获取 UL 的宽度

标签 javascript jquery html css

这是我无法获得 <ul> 宽度的问题.

html文件:

<ul>
    <li>image goes here</li>
    <li>image goes here</li>
    <li>image goes here</li>
</ul>

CSS文件:

ul {
    display: inline-block;
    white-space: nowrap;
}

 li {
    width: 90px;
    height: 90px;
    display: inline-block;
}

这里是 Jquery 代码的例子:

$(document).ready(function(){
    var $width = $('ul').width();
    console.log('width: ' + $width); 
});

它返回 0 如何获得 ul 宽度?

最佳答案

使用 CSS 函数 csswdith 作为属性:

$('ul').css("width");

$(document).ready(function() {
  var $width = $('ul').css("width");
  console.log('width: ' + $width);
});
ul {
  display: inline-block;
  white-space: nowrap;
}

li {
  width: 90px;
  height: 90px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>image goes here</li>
  <li>image goes here</li>
  <li>image goes here</li>
</ul>

关于javascript - 获取 UL 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46387686/

相关文章:

Javascript 字符串编码 Windows-1250 到 UTF8

javascript - Jquery 在公共(public)父级中选择 div

javascript - UI-Router 和解析, Controller 中的未知提供程序

javascript - 初始化 AngularJS Controller 时出现问题 : Cannot set property . .. 未定义

javascript - jQuery 滑出问题

javascript - 如何删除退格键上插入的<span>?

html - Firefox 显示主菜单和下拉菜单之间的差距,行高增加

javascript - javascript可以从父页面访问iframe元素吗?

javascript - 解析 json 并将其存储在数组中?

html - 为什么在 IE 11 中垂直滚动条的底部被截断了?