如何获取没有定义宽度的元素的宽度?
你如何找出被 20 个其他元素包裹的元素的宽度,但我知道的唯一固定宽度是主包装器的宽度,即 800px。所有子元素通常都是块,无论是否浮动,具有不同的填充和边距。
我真的不需要特定案例的答案,我只是想知道是否有工具或技巧可以快速计算此类事情。
谢谢
编辑:抱歉忘记了,这是在外部网站上,我无法安装任何脚本或编辑 CSS。
How would you find out the width of a element that is wrapped by 20 odd other elements, but the only fixed width I know is the main wrapper's which is 800px. All child elements are generally blocks, floating or not, with different paddings and margins.
I don't really need the answer to a specific case, I'm just wondering if there are tools or tricks to quickly calculate such things.
Thanks
EDIT: Sorry to forget, this is on an external site where I can not install any scripts or edit the css.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Firebug 是你的朋友。
How do you get the width of an element without a defined width? - Stack Overflow http://img.skitch.com/20100426-nequkipg8ex3khs1mux3tnctjb.preview.jpg
Firebug is your friend.
How do you get the width of an element without a defined width? - Stack Overflow http://img.skitch.com/20100426-nequkipg8ex3khs1mux3tnctjb.preview.jpg
jQuery 有 width() ,它允许跨浏览器可靠地找出任何 DOM 元素的宽度。这有帮助吗?
jQuery has width() that allows to find out the width of any DOM element reliably across browsers. Does that help?
您需要使用
element.offsetWidth
。You need to use
element.offsetWidth
.你可以把JQuery来使用并使用它的
width()
方法。 jQuery 足够智能,可以计算出您指定的元素的宽度。You can put JQuery to use and use its
width()
method for that. The jQuery is smart enough to get the computed width of the element you specify.