In various cases JS gets the element width high

Source: Internet
Author: User

In all cases JS gets the element width high

For the sake of the narrative simplicity, here only the width example is taken.

  Scenario one, the element style property is set to Width/height

<div style= "width:996px" >test<div>
<script>
var div = document.getelementsbytagname (' div ') [0];
alert (div.style.width);
</script> Default Classification

As above, you can use El.style.width.

  

If width is not set in the Style property, then using El.style.width will not get to the following

<div>test<div>
<script>
var div = document.getelementsbytagname (' div ') [0];
alert (div.style.width);
</script>

All browsers pop out an empty string. Even if the style is embedded in the CSS of the page, it is still not available, as follows

<style>
div {width:100px}
</style>


<div>test<div>
<script>
var div = document.getelementsbytagname (' div ') [0];
alert (div.style.width);
</script>

This time getComputedStyle or Currentstyle will come in handy.

  Scenario two, elements are set by introducing style sheets Width/height

There are two ways to introduce a style sheet, one is to use the link tag to introduce a separate CSS file, and the other is to use the style tag directly in the HTML page. This is the second way of testing. As follows

<style>
div {width:100px}
</style>
<div>test<div>
<script>
function GetStyle (el, name) {
if (window.getComputedStyle) {
Return window.getComputedStyle (EL, null);
}else{
return el.currentstyle;
}
}
var div = document.getelementsbytagname (' div ') [0];
Alert (GetStyle (Div, ' width '));
</script>

100px is ejected from all browsers. Description through getComputedStyle and currentstyle you can get to the width of the element defined in the style sheet.

What if the element does not have a wide height set in the Style property, does not set the width height in the stylesheet, can also be obtained with getComputedStyle or Currentstyle? The answer is getComputedStyle,Currentstyle can not . As follows

<div>test<div>
<script>
function GetStyle (el, name) {
if (window.getComputedStyle) {
Return window.getComputedStyle (EL, null);
}else{
return el.currentstyle;
}
}
var div = document.getelementsbytagname (' div ') [0];
Alert (GetStyle (Div, ' width '));
</script>

The div neither sets the style property nor introduces a style sheet. You can get the width height in firefox/ie9/safari/chrome/opera (browser default), but not in Ie6/7/8, the return is auto.

Note that the GetStyle method takes precedence over getComputedStyle, and IE9 already supports the method. So the IE9 can get to the wide height. However, IE6/7/8 is not supported and can only be obtained using Currentstyle.

In various cases JS gets the element width high

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.