javascript - 如何从javascript获取内联 block 字段集的显示宽度

标签 javascript html css fieldset

如果你有一个带有 display: inline-block 的字段集,它的宽度取决于它包含的元素。您可以看到它,但假设:

<fieldset id='fs' style='display: inline-block'>
  <input type=number>
</fieldset>

<script>
  var fs=document.getElementById("fs");
  console.log("fs.width:",fs.width);
</script>

唯一的输出将是:

fs.width: undefined

在尝试了很多东西之后,我终于能够像这样得到它:

<script>
  var fs=document.getElementById("fs");
  var fsrects=fs.getClientRects();
  var fswidth=fsrects[0].right-fsrects[0].left;
  console.log("fswidth:",fswidth);
</script>

然后输出是

fswidth: 98.6640625

一定有更简单的方法!帮忙!!!

最佳答案

您可能正在寻找 offsetWidth .

The HTMLElement.offsetWidth read-only property returns the layout width of an element. Typically, an element's offsetWidth is a measurement which includes the element borders, the element horizontal padding, the element vertical scrollbar (if present, if rendered) and the element CSS width.

  var fs=document.getElementById("fs");
  console.log("fs.width:",fs.offsetWidth);
<fieldset id='fs' style='display: inline-block'>
  <input type=number>
</fieldset>

关于javascript - 如何从javascript获取内联 block 字段集的显示宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45747675/

相关文章:

java - 在 Thymeleaf 中,如何获取 HTML 变量并将其发送到 Controller ?

javascript - 从javascript获取DOM元素信息

javascript - 如何通过双击加载而不是单击放大弹出窗口

jquery - 使用 JqueryUI 下拉菜单显示单个 div

javascript - 使用 IE 和 Chrome 卡住盒子

javascript - 发出一系列 (n) AJAX 调用的结束信号

javascript - 如何使用 jQuery 拆分字符串并将其包装在 span 标签中,然后输出新代码?

javascript - HTML 表使用 jQuery 查找所有选中的行

javascript - Shadertoy 的音频着色器如何工作?

html - 自定义字体未显示在我的网络预览中