JavaScript dom_ gets the CSS style setting element size

Source: Internet
Author: User

I. Getting the size of an element through a style inline

The style gets the width and height of the CSS style that can only be obtained into the inline style property, if there is a fetch, or null if none.

<script type= "Text/javascript" >    function() {           var// get element        // 200px, empty        if not set // 200px, empty if not set     }</script>

Second, the size of the element obtained by calculation

  The calculation gets the size of the element, regardless of whether you are inline, inline, or linked, and it is calculated to return the results.
If the size itself is set, it returns the size of the element, and if itself is not set, the default size is returned, and the ie6,7,8 browser returns auto.

<script type= "Text/javascript" >    function() {        varnull Null | | Box.currentstyle;        alert (style.width);         alert (style.height);     } </script>

Third, get the element size by the Cssrules (or rules) property in the Cssstylesheet object

Cssrules (or rules) can only get the width and height of inline and link styles, and cannot get into inline and computed styles.

<script type= "Text/javascript" >window.onload=function(){           varsheet = document.stylesheets[0];//get link or style        varRule = (Sheet.cssrules | | sheet.rules) [0];//get the first rulealert (rule.style.width);//200px, Emptyalert (rule.style.height);//200px, Empty    }    </script><style type= "Text/css" >. aaa{background: #ccc;        width:200px;    height:200px; }</style>

JavaScript dom_ gets the CSS style setting element size

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.