The element is not shown using currentStyle in IE when width and height is set to auto

  • 2020-03-30 02:49:09
  • OfStack

We know that you can use the currentStyle attribute in IE to get the actual width and height of the element. However, if there is no display to set the width and height of the element, then using this attribute will not get the value of auto. The following
 
<div>abcd</div> 
<script> 
var div = document.getElementsByTagName('div')[0]; 
alert(div.currentStyle.width); 
alert(div.currentStyle.height); 
</script> 

The output from IE6/7/8/9 is auto. If the width and height are set, the output is the actual width and height. The following

1, set by inline style property
 
<div style="width:100px;height:50px;">abcd</div> 
<script> 
var div = document.getElementsByTagName('div')[0]; 
alert(div.currentStyle.width); 
alert(div.currentStyle.height); 
</script> 

2, through the page embedded style tag Settings
 
<style> 
div { 
width: 100px; 
height: 50px; 
} 
</style> 
<div>abcd</div> 
<script> 
var div = document.getElementsByTagName('div')[0]; 
alert(div.currentStyle.width); 
alert(div.currentStyle.height); 
</script> 

Output: 100px, 50px

Related articles: