Prototype - getDimensions() 方法



此方法查詢元素的計算寬度和高度,並將其作為物件的鍵/值對返回。

此方法將針對 display 在內聯樣式規則或 CSS 樣式表中均設定為 none 的元素返回正確的值。

請注意,雖然結果以畫素表示,但所返回的值是一個數字。

語法

element.getDimensions();

返回值

其返回物件 {height: number, width: number} 的鍵/值對。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var dimensions = $('rectangle').getDimensions();
            alert("Element width is " +  dimensions.width );
            alert("Element height is " +  dimensions.height );
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      
      <div id = "rectangle" 
         style = "font-size: 10px; width: 20em; height: 10em">
         <p>This is the paragraph.</p>
      </div>
      <br />
      
      <input type = "button" value = "showResult" onclick = "showResult();"/>
   </body>
</html>

輸出

prototype_element_object.htm
廣告
© . All rights reserved.