JS method to obtain an unknown DIV height

Source: Internet
Author: User
This article mainly introduces how to obtain an unknown DIV height by JS. It involves javascript techniques related to dynamic operations on page element attributes and has some reference value, for more information about how to obtain the height of an unknown DIV, see the example in this article. We will share this with you for your reference. The details are as follows:

The height of an element can be obtained through the clientHeight attribute of the element, for example:

var height = element.clientHeight;

Limitations of this approach:

1. If the display attribute of the element is set to none, the result is 0.

2. In safari, you need to use element. offsetHeight to get the actual height. This is a bug in safari.

The methods provided by Prototype are as follows:

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};}

I hope this article will help you design JavaScript programs.


For more information about how to obtain an unknown DIV height using JS, see PHP!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.