此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

Document.width

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

已棄用: 不推薦使用此功能。雖可能有一些瀏覽器仍然支援它,但也許已自相關的網頁標準中移除、正準備移除、或僅為了維持相容性而保留。避免使用此功能,盡可能更新現有程式;請參考頁面底部的相容性表格來下決定。請注意:本功能可能隨時停止運作。

備註: 從 Gecko 6.0 開始, document.width 將不再被支援。取而代之的是 document.body.clientWidth。請參照:element.clientWidth.

傳回目前文件中,<body> 元素的寬度有多少像素。

Internet Explorer 不支援!

語法

js
pixels = document.width;

範例

js
function init() {
  alert("文件的寬度是 " + document.width + " 像素。");
}

其他替代

js
document.body.clientWidth; /* <body> 的寬度 */
document.documentElement.clientWidth; /* <html> 的寬度 */
window.innerWidth; /* 視窗的寬度 */

規範於

HTML5

同時參考