jQuery - outerwidth( [margin] ) 方法



描述

outerWidth( [margin] ) 方法獲取第一個匹配元素的外寬度(預設情況下包括邊框和內邊距)。

此方法適用於可見和隱藏元素。它不支援由於父元素隱藏而間接隱藏的元素。

語法

以下是使用此方法的簡單語法:

selector.outerWidth( [margin] )

引數

以下是此方法使用所有引數的描述:

  • margin - 當此可選引數設定為 true 時,元素的邊距將包含在計算中。

示例

以下是一個簡單的示例,展示了此方法的用法:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://tutorialspoint.tw/jquery/jquery-3.6.0.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
			
            $("div").click(function () {
               var color = $(this).css("background-color");
               var width = $(this).outerWidth( true );
               $("#result").html("Outer Width is <span>" + width + "</span>.");
               $("#result").css({'color': color, 'background-color':'gray'});
            });
				
         });
      </script>
		
      <style>
         #div1{ margin:10px;padding:12px; border:2px solid #666; width:60px;}
         #div2 { margin:15px;padding:5px; border:5px solid #666; width:60px;}
         #div3 { margin:20px;padding:4px; border:4px solid #666; width:60px;}
         #div4 { margin:5px;padding:3px; border:3px solid #666; width:60px;}
      </style>
   </head>
	
   <body>
      <p>Click on any square:</p>
      <span id = "result"> </span>
		
      <div id = "div1" style = "background-color:blue;"></div>
      <div id = "div2" style = "background-color:pink;"></div>
      <div id = "div3" style = "background-color:#123456;"></div>
      <div id = "div4" style = "background-color:#f11;"></div>
   </body>
</html>

這將產生以下結果:

jquery-css.htm
廣告

© . All rights reserved.