mark: firefox獲取dom節點css屬性

https://developer.mozilla.org/En/DOM/Window.getComputedStyle
以前寫過一個笨方法來獲取,ff自身就提供了一個方法(獲取自firebug)
  1. <html>
  2.     <head>
  3.         <style>
  4.         div {
  5.             border: 1px solid red;  
  6.         }
  7.         #main {
  8.             width: 400px;
  9.             height: 300px;
  10.         }   
  11.         #top {
  12.             margin: 20px;
  13.             padding: 10px;  
  14.             height: 50px;
  15.         }
  16.         </style>
  17.     </head>
  18.     <body>
  19.         <div id="main">
  20.             <div id="top">
  21.                 Hello
  22.             </div>
  23.         </div>
  24.     </body>
  25.     <script type="text/javascript">
  26.     function $(id) {
  27.         return document.getElementById(id);
  28.     }
  29.     function test() {
  30.         var win = $('top').ownerDocument.defaultView;
  31.         var style = win.getComputedStyle($('top'), "");
  32.         var val = style.getPropertyCSSValue('margin-top').cssText;
  33.         alert(val);
  34.     }
  35.     test();
  36.     </script>
  37. </html>

發佈了23 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章