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
var previousCss = $("#myDiv").attr("style");
$("#myDiv").css({
position: 'absolute',
visibility: 'hidden',
display: 'block'
});
var optionHeight = $("#myDiv").height();
$("#myDiv").attr("style", previousCss ? previousCss : "");
This method allows you to discreetly measure the height of a hidden element without affecting the layout or visibility of the page.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3