简体   繁体   English

无法获得Firefox中隐藏div的高度

[英]Can't get height of hidden div in Firefox

I'm trying to get the height of an initially hidden div element in Netscape and Firefox using javascript (it works fine in IE). 我正在尝试使用javascript获取Netscape和Firefox中最初隐藏的div元素的高度(在IE中工作正常)。 I have tried using MyElement.scrollHeight, MyElement.offetHeight, MyElement.style.height and many different ways of setting the element initially visible, getting the height and then hiding it again. 我尝试使用MyElement.scrollHeight,MyElement.offetHeight,MyElement.style.height以及许多不同的方法来设置元素初始可见,获取高度然后再次隐藏它。 I keep getting a value of 0. Any thoughts? 我的值一直为0。有什么想法吗?

Correct me if I'm wrong (Which I may be), but from doing some research, elements with display: none have no height. 如果我错了(可能是我),请纠正我,但是从做一些研究后,显示元素:没有高度。

To get the height, you would need to unhide them, get the height, then rehide them. 要获得高度,您需要取消隐藏它们,获得高度,然后重新隐藏它们。 Issues like this have popped up in the past like so: 这样的问题在过去像这样突然出现:

jQuery: height()/width() and "display:none" jQuery:height()/ width()和“ display:none”

jQuery: Get height of hidden element in jQuery jQuery:获取jQuery中隐藏元​​素的高度

(Both jQuery examples, but you get the point) (两个jQuery示例,但您都明白了)

Hidden elements in Netscape is not accessible. Netscape中的隐藏元素不可访问。

you can set that position with styling hole of div out of your screen. 您可以在屏幕外使用div的样式孔设置该位置。 for example move it to -9999 on top left. 例如,将其移动到左上方的-9999。 then you can access that's attributes like height and width. 那么您可以访问诸如高度和宽度之类的属性。

you can also visible that element and get your attribute and hide it again, but it is not usual because your element will be show a little bit of the second and user maybe feel jumping on screen. 您也可以看到该元素并获取您的属性并再次隐藏它,但这并不常见,因为您的元素将显示一秒钟,并且用户可能会在屏幕上跳跃。
you should position it by setting style to "absolute" and "top" to -9999px or more (depend of your project height) and then get your element height and width with DOM and anythings that you want ... 您应该通过将样式设置为“绝对”,将“顶部”设置为-9999px或更大(取决于您的项目高度)来定位它,然后使用DOM和您想要的任何东西获取元素的高度和宽度...

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

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