Home >Web Front-end >CSS Tutorial >How Can I Efficiently Get the Height of an Element Inside a Hidden Div Using jQuery?
Obtaining the height of an element concealed within a hidden div can be challenging. The conventional method involves displaying the div, retrieving the height, and then hiding it, which can be cumbersome.
However, there is a more efficient approach that leverages jQuery's ability to modify CSS styles:
Modify CSS: Temporarily set the following CSS properties for the hidden div:
var previousCss = $("#myDiv").attr("style"); $("#myDiv").css({ position: 'absolute', // Optional if #myDiv is already absolute visibility: 'hidden', display: 'block' }); optionHeight = $("#myDiv").height(); $("#myDiv").attr("style", previousCss ? previousCss : "");
This approach provides a more efficient and flexible way to obtain the height of hidden elements, without having to repeatedly show and hide the parent div.
The above is the detailed content of How Can I Efficiently Get the Height of an Element Inside a Hidden Div Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!