jQuery:隐藏元素 - 一般问题
当元素被隐藏时,您无法读取例如。它们的尺寸,这是一般的 javascript 问题还是 jQuery 中可能有解决方法?
我有例如。一些包含小部件的选项卡,初始化它们无法正常工作,因为它们是隐藏的。
谢谢
when elements are hidden, you can't read eg. their dimensions, is this a general javascript problem or is there maybe a workaround in jQuery?
i'm having eg. some tabs which contain widgets, initializing them doesn't work properly since they're hidden.
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据我所知,没有解决方法。
但是,您可以在 CSS 中使用visibility:hidden 而不是display:none。使用visibility:hidden隐藏的元素保持其位置和大小,但只是不可见。
希望这有帮助。
No workaround that I know of.
You could, however, use visibility:hidden instead of display:none in your CSS. Elements that are hidden using visibility:hidden maintain their position and size but are simply invisible.
Hope this helps.
这个尺寸插件说它可以获取隐藏元素的尺寸。
This dimension plugin says it can get the dimensions of hidden elements.
如果您设置visibility:hidden,该元素仍然占用空间,因此还要设置position:absolute以将其移出文档流。如有必要,您可以将元素放置在屏幕之外,这样它就不会无形地阻止事件等。
从那里您仍然可以读取元素的宽度。例如:
希望这有帮助!
If you set visibility:hidden the element still takes up space, so set position:absolute as well to move it out of the document flow. If necessary you can position the element off the screen so that it does not invisibly block events and such.
From there you can still read the element's width. For example:
Hope this helps!
这篇博客文章重点介绍了一种使用 display:none 获取隐藏元素尺寸的方法。这里的其他答案涉及相同的想法,但都没有提供将 CSS 属性恢复为其原始值的解决方案。
This blog post highlights a way to get dimensions of an element that is hidden using display:none. The other answers here touch on the same ideas, but both don't offer a solution to reverting the CSS properties back to their original values.