當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JQuery .width()用法及代碼示例

獲取匹配元素集中第一個元素的當前計算寬度或設置每個匹配元素的寬度。

用法一

.width() => Number

說明:獲取匹配元素集中第一個元素的當前計算寬度。

  • 添加的版本:1.0.width()

    • 此方法不接受任何參數。

.css( "width" ).width() 之間的區別在於後者返回一個 unit-less 像素值(例如 400 ),而前者返回一個單位完整的值(例如 400px )。當需要在數學計算中使用元素的寬度時,建議使用.width() 方法。

圖 1 - 測量寬度示意圖

此方法還能夠找到窗口和文檔的寬度。

// Returns width of browser viewport
$( window ).width();
 
// Returns width of HTML document
$( document ).width();

請注意,無論 CSS box-sizing 屬性的值如何,.width() 將始終返回內容寬度。從 jQuery 1.8 開始,這可能需要檢索 CSS 寬度加上 box-sizing 屬性,然後當元素具有 box-sizing: border-box 時減去每個元素上的任何潛在邊框和填充。為避免這種懲罰,請使用 .css( "width" ) 而不是 .width()

注意:雖然stylescript標簽將報告一個值.width()或者height()當絕對定位和給定display:block,強烈建議不要在這些標簽上調用這些方法。除了是一種不好的做法外,結果也可能被證明是不可靠的。

其他注意事項:

  • dimensions-related API(包括 .width() )返回的數字在某些情況下可能是小數。代碼不應假定它是整數。此外,當用戶縮放頁麵時,尺寸可能不正確;瀏覽器不會公開 API 來檢測這種情況。
  • 當元素或其父元素被隱藏時,.width() 報告的值不能保證準確。要獲得準確的值,請確保在使用 .width() 之前元素可見。 jQuery 將嘗試暫時顯示然後重新隱藏元素以測量其尺寸,但這是不可靠的,並且(即使準確)會顯著影響頁麵性能。這個 show-and-rehide 測量函數可能會在 jQuery 的未來版本中被刪除。

例子:

顯示各種寬度。請注意,這些值來自 iframe,因此可能比您預期的要小。黃色高亮顯示 iframe 主體。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>width demo</title>
  <style>
  body {
    background: yellow;
  }
  button {
    font-size: 12px;
    margin: 2px;
  }
  p {
    width: 150px;
    border: 1px red solid;
  }
  div {
    color: red;
    font-weight: bold;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<button id="getp">Get Paragraph Width</button>
<button id="getd">Get Document Width</button>
<button id="getw">Get Window Width</button>
<div>&nbsp;</div>
<p>
  Sample paragraph to test width
</p>
 
<script>
function showWidth( ele, w ) {
  $( "div" ).text( "The width for the " + ele + " is " + w + "px." );
}
$( "#getp" ).click(function() {
  showWidth( "paragraph", $( "p" ).width() );
});
$( "#getd" ).click(function() {
  showWidth( "document", $( document ).width() );
});
$("#getw").click(function() {
  showWidth( "window", $( window ).width() );
});
</script>
 
</body>
</html>

演示:

用法二

.width( value ) => jQuery

說明:設置匹配元素集中每個元素的 CSS 寬度。

  • 添加的版本:1.0.width( value )

    • value
      類型:StringNumber
      一個表示像素數的整數,或一個整數以及附加的可選測量單位(作為字符串)。
  • 添加的版本:1.4.1.width( function )

    • function
      類型:Function(Integer 索引,Integer 值)=> StringNumber
      返回要設置的寬度的函數。接收集合中元素的索引位置和舊寬度作為參數。在函數中,this 指的是集合中的當前元素。

調用 .width("value") 時,該值可以是字符串(數字和單位)或數字。如果隻為該值提供了一個數字,則 jQuery 假定一個像素單位。但是,如果提供了字符串,則任何有效的 CSS 測量值都可以用於寬度(例如 100px50%auto )。請注意,在現代瀏覽器中,CSS 寬度屬性不包括填充、邊框或邊距,除非使用了 box-sizing CSS 屬性。

如果未指定明確的單位(如"em" 或"%"),則假定為"px"。

請注意,無論 CSS box-sizing 屬性的值如何,.width("value") 都會設置框的內容寬度。

例子:

第一次單擊時更改每個 div 的寬度(並更改其顏色)。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>width demo</title>
  <style>
  div {
    width: 70px;
    height: 50px;
    float: left;
    margin: 5px;
    background: red;
    cursor: pointer;
  }
  .mod {
    background: blue;
    cursor: default;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<div>d</div>
<div>d</div>
<div>d</div>
<div>d</div>
<div>d</div>
 
<script>
var modWidth = 50;
$( "div" ).one( "click", function() {
  $( this ).width( modWidth ).addClass( "mod" );
  modWidth -= 8;
});
</script>
 
</body>
</html>

演示:

相關用法


注:本文由純淨天空篩選整理自jquery.com大神的英文原創作品 .width()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。