Home > Article > Web Front-end > How to Get the Height of a Hidden Element with jQuery?
Retrieving Height of Concealed Elements with jQuery
When working with hidden elements, it may be necessary to retrieve their dimensions for various purposes. However, conventional methods of obtaining the height of an element can fail if the element is not visible.
Inefficient Approach
The described approach of temporarily unhiding the element, measuring its height, and then re-hiding it is cumbersome and inefficient.
Alternative Solution
jQuery provides a more efficient solution:
Modify Element Attributes: Temporarily manipulate the element's style attributes:
Code Example
<code class="javascript">var previousCss = $("#myDiv").attr("style"); $("#myDiv").css({ position: 'absolute', visibility: 'hidden', display: 'block' }); var optionHeight = $("#myDiv").height(); $("#myDiv").attr("style", previousCss ? previousCss : "");</code>
This method allows you to discreetly measure the height of a hidden element without affecting the layout or visibility of the page.
The above is the detailed content of How to Get the Height of a Hidden Element with jQuery?. For more information, please follow other related articles on the PHP Chinese website!