简体   繁体   English

获取DOM元素的宽度?

[英]Get the width of a DOM element?

Is there a way of accessing the width of a DOM element? 有没有办法访问DOM元素的宽度? In particular, I'm looking to get the width of a table cell (td) who's width can be of varying lengths based on the length of the text in that cell. 特别是,我希望获得一个表格单元格(td)的宽度,该表格单元格的宽度可以根据该单元格中文本的长度而变化。

ideally, the solution I'm looking for will use the jQuery library, but others will work as well. 理想情况下,我正在寻找的解决方案将使用jQuery库,但其他解决方案也可以使用。

Also, I must stress that the solution must work in IE6 (unfortunately :-P ) in addition to ie7, ie8, and firefox. 另外,我必须强调,除了ie7,ie8和firefox之外,该解决方案还必须在IE6中运行(不幸的是:-P)。 Thanks in advance!! 提前致谢!!

$('#td_id').width();

当然,您必须精心设计选择器才能使用所讨论的单元格,但否则就这么简单!

$(selector).width() should work fine, but make sure for IE6 and below that the page is rendered in Standards mode and not Quirks-Mode (so it follows W3C box model). $(selector).width()应该可以正常工作,但请确保对于IE6及以下版本,页面以“标准”模式而不是“ Quirks-Mode”(因此遵循W3C框模型)呈现。

Here is some information on how to toggle Standard/Strict Mode: 以下是有关如何切换标准/严格模式的一些信息:

CSS - Quirks mode and strict mode CSS-怪癖模式和严格模式

您是否尝试过jQuery中“宽度”方法

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

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