在HTML和Web开发中,window 对象代表浏览器窗口。一些常用的 window 方法

1.alert(): 显示一个对话框,带有一段消息和确定按钮。

window.alert("Hello, World!");

 2.confirm(): 显示一个对话框,带有一段消息和两个按钮(确定和取消)。

var result = window.confirm("Are you sure?");
if (result) {
  console.log("You clicked OK!");
} else {
  console.log("You clicked Cancel!");
}

3.prompt(): 显示一个对话框,带有一段消息和输入框 

var result = window.prompt("Please enter your name:", "Harry Potter");
if (result !== null) {
  console.log("Hello, " + result + "!");
}

 4.open(): 打开一个新的浏览器窗口或标签页。

window.open('https://www.example.com', '_blank');

 5.close(): 关闭当前窗口。注意,出于安全原因,这个函数通常只能关闭由脚本打开的窗口。

window.close();

6.print(): 打印文档。

window.print();

7. focus(): 将焦点设置到当前窗口。

window.focus();

8.blur(): 移除当前窗口的焦点。

window.blur();

9. scrollTo(): 滚动到文档中的特定位置。

window.scrollTo(0, 0); // 滚动到页面顶部

 10.scrollBy(): 相对于当前位置滚动文档。

window.scrollBy(0, 500); // 向下滚动500像素

11. setTimeout(): 设置一个定时器,当定时器到期时执行函数。

setTimeout(function() {
  console.log("This message is shown after 3 seconds");
}, 3000);

12.clearTimeout(): 取消由 setTimeout() 设置的定时器。 

var timerId = setTimeout(function() {
  console.log("This won't be shown");
}, 3000);
clearTimeout(timerId);

13.setInterval(): 设置一个定时器,按照指定的时间间隔周期性地执行函数。 

setInterval(function() {
  console.log("This message is shown every 2 seconds");
}, 2000);

14.clearInterval(): 取消由 setInterval() 设置的定时器。

var intervalId = setInterval(function() {
  console.log("This won't be shown anymore");
}, 2000);
clearInterval(intervalId);

15.addEventListener(): 给 window 对象添加事件监听器。

window.addEventListener('resize', function() {
  console.log("Window has been resized");
});

16. removeEventListener(): 从 window 对象移除事件监听器。

window.removeEventListener('resize', someFunction);

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值