jQuery获得document和window对象宽度和高度的方法
网络编程 2025-03-14 09:50www.168986.cn编程入门
jQuery技巧:获取文档和窗口的宽度与高度
在网页开发中,我们经常需要获取文档(document)和窗口(window)的宽度和高度。使用jQuery可以方便地实现这一需求。本文将通过实例讲解如何使用jQuery获取document和window对象的宽度和高度,分享给大家作为参考。
我们需要引入jQuery库。在HTML文档的
部分加入以下代码:```html
```
接着,我们可以使用jQuery的$(document).ready()函数来确保在文档加载完成后执行相关操作。在
$(document).ready(function(){
$("button").click(function(){
var txt = "";
txt += "Document width/height: " + $(document).width() + "x" + $(document).height() + "";
txt += "Window width/height: " + $(window).width() + "x" + $(window).height();
alert(txt);
});
});