ボタンをクリックすると、横幅を表示します。
サンプルプログラムはiframe内で動いているので、windowの横幅は黄色い部分の値になります。予期したよりも小さい値が表示されているかもしれません。その場合は、単独で開いて確認してみて下さい。
Css/API/jQuery
width()
最初の要素の横幅をピクセル単位で取得します。
jQuery1.2からは、このメソッドでwindowやdocumentの横幅も取得できるようになりました。
jQuery1.2からは、このメソッドでwindowやdocumentの横幅も取得できるようになりました。
サンプル
サンプル1
<button id="getp">Get Paragraph Width</button> <button id="getd">Get Document Width</button> <button id="getw">Get Window Width</button> <div> </div> <p> Sample paragraph to test width </p>
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());
});
[全コードを表示]
[実行結果を単体で表示]