原型 - getWidth() 方法



此方法查詢並返回元素的已計算寬度。

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

請注意,返回的值是一個數字,儘管它以畫素為單位表示。

語法

element.getWidth();

返回值

它返回元素的已計算寬度。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var width = $('rectangle').getWidth();
            alert("Element width is " +  width );
         }
      </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
廣告