Home >Web Front-end >CSS Tutorial >How Can I Efficiently Get the Height of a Hidden Element Using jQuery?
Getting Height of Hidden Elements with jQuery
Determining the height of an element hidden within a concealed parent div can be challenging. It's inefficient to temporarily reveal the element just to obtain its height and then hide it again. Here's a more optimal solution:
Solution:
You can use the following code snippet:
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 : "");
Explanation:
This approach avoids the need for showing and hiding the parent div, resulting in a more efficient way to get the height of the hidden element.
The above is the detailed content of How Can I Efficiently Get the Height of a Hidden Element Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!