This article describes how JS obtains an unknown DIV height. Share it for your reference, as follows:
The height of the element can be obtained through the clientHeight attribute of the element, such as:
var height = element.clientHeight;
Limitations of this practice:
1. If the display attribute of the element is set to none, the result is 0
2. In the safari browser, you need to use: element.offsetHeight to get the actual height, which is a bug in the safari browser.
The following is the method provided by Prototype, which is compatible with various browsers, and can also correctly obtain element sizes when elements are hidden, for reference:
getDimensions: function(element) { element = $(element); var display = $(element).getStyle('display'); if (display != 'none' && display != null) // Safari bug return {width: element.offsetWidth, height: element.offsetHeight}; // All *Width and *Height properties give 0 on elements with display none, // so enable the element temporarily var els = element.style; var originalVisibility = els.visibility; var originalPosition = els.position; var originalDisplay = els.display; els.visibility = 'hidden'; els.position = 'absolute'; els.display = 'block'; var originalWidth = element.clientWidth; var originalHeight = element.clientHeight; els.display = originalDisplay; els.position = originalPosition; els.visibility = originalVisibility; return {width: originalWidth, height: originalHeight};}For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.