JavaScript中的CSS樣式編程——最終樣式

來源:互聯網
上載者:User

 

關於最終樣式,IE和DOM在各自的實現上存在差異。

1.IE中的最終樣式,代碼如下:

<html><br /> <head><br /> <title>Accessing Style Sheets Example</title><br /> <style type="text/css"><br /> div.special {<br /> background-color: red;<br /> height: 10px;<br /> width: 10px;<br /> margin: 10px;<br /> }<br /> </style><br /> <script type="text/javascript"><br /> function getBackgroundColor() {<br /> var oDiv = document.getElementById("div1");<br /> alert(oDiv.currentStyle.backgroundColor);<br /> }<br /> </script></p><p> </head><br /> <body><br /> <div id="div1" class="special"></div><br /> <input type="button" value="Get Background Color" onclick="getBackgroundColor()" /><br /> <p>The alert displays nothing because the background color of the <code>div</code> is assigned by a class, not by the <code>style</code> attribute.</p><br /> </body><br /></html>

2.DOM中的最終樣式:

<html><br /> <head><br /> <title>Accessing Style Sheets Example</title><br /> <style type="text/css"><br /> div.special {<br /> background-color: red;<br /> height: 10px;<br /> width: 10px;<br /> margin: 10px;<br /> }<br /> </style><br /> <script type="text/javascript"><br /> function getBackgroundColor() {<br /> var oDiv = document.getElementById("div1");<br /> alert(document.defaultView.getComputedStyle(oDiv, null).backgroundColor);<br /> }<br /> </script></p><p> </head><br /> <body><br /> <div id="div1" class="special"></div><br /> <input type="button" value="Get Background Color" onclick="getBackgroundColor()" /><br /> <p>The alert displays nothing because the background color of the <code>div</code> is assigned by a class, not by the <code>style</code> attribute.</p><br /> </body><br /></html>

 

相關文章

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.