原型 - getStyle() 方法



此方法返回元素的給定 CSS 屬性值。屬性可以採用 CSS 或駝峰命名形式。

所以提供 font-sizefontSize 等效。

語法

element.getStyle( property );

返回值

如果找不到屬性集則返回 CSS 屬性值或者 NULL。Internet Explorer 返回字面值,而其他瀏覽器返回計算值。如果元素處於隱藏狀態,Safari 將為任何非內聯屬性返回 null。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var str = $('grandfather').getStyle('margin-left');
            alert("Element left margin is : " + str );
         }
      </script>
      
      <style>
         #grandfather {
            font-size: 12px;
            margin-left: 1em;
         }
      </style>
   </head>
   
   <body>
      <p>Click the button to see the result.</p>
      
      <div id = "grandfather">This is grand father
         <div id = "father"> This is father.
            <div id = "kid">This is kid</div>
         </div>
      </div>
      <br />
      
      <input type = "button" value = "showResult" onclick = "showResult();"/>
   </body>
</html>

輸出

prototype_element_object.htm
廣告
© . All rights reserved.