JavaScript Tutorial/Style/Computed Style

Материал из Web эксперт
Перейти к: навигация, поиск

Get Computed Style

This example uses the DOM style methods and will fail in Internet Explorer.
<html>
    <head>
        <title>Computed Style Example</title>
        <style type="text/css">
            div.special {
                background-color: red;
            }
        </style>
        <script type="text/javascript">
            function getBackgroundColor() {
                var oDiv = document.getElementById("div1");
                alert(document.defaultView.getComputedStyle(oDiv, null).backgroundColor);
            }
        </script>
 
    </head>
    <body>
        <div id="div1" class="special"></div>
        <input type="button" value="Get Background Color" onclick="getBackgroundColor()" />
    </body>
</html>