JS-BOM操作(二)
弹出框
警告框:弹出提示,只能点击确认或×
确认框:弹出提示,可以选择确认或者取消,确认则返回true,取消返回false
提示输入框:可以进行输入,点击确认返回输入值,点击取消返回NULL
//警告框alert,可以不带alert
window.alert("");
//确认框confirm确认框,
//点击确定则返回true,点击取消返回false
window.confirm("sometext");
//prompt
//提示输入,如果点击确定,返回输入值,如果点击取消返回NULL
window.prompt("请输入","1234");
//如果框需要换行在中间加一个\n进行
Timing事件(定时器)
setImeout:只进行一次定时,时间一过就触发函数,且不再执行
clearInterval:进行多次,每隔一段时间就触发相应函数
//window.setTimeout()方法定时器
//setTimeout(function, milliseconds)
//function是要执行的函数,第二个参数是执行之前的毫秒数
let time =setTimeout(function () {
alert("0.3秒弹出一次");
},300);
//clearTimeout()停止setTime()中规定的函数
//window可以不带
window.clearTimeout(time);
//setInterval方法
//setInterval(function, milliseconds)每个给定的时间间隔重复给定的函数
//function是要执行的函数,第二个参数是执行之前的毫秒数
let inte = setInterval(function () {
alert("0.4秒弹出")
},400);
//clearInterval清除
clearInterval(inte);
Cookie
1.创建cookie:document.cookie="";
2.读取cookie;存到一个变量中
3.删除:设置 expires 参数为以前的时间即可,浏览器就自动将其清除
//创建cookie
document.cookie = "username =bill Gate";
//添加有效日期
document.cookie ="username = john ;expires= Sun,31 Dec 2021 20:00:00 UTC";
//path参数,告诉浏览器cookie属于什么路径,默认当前页
document.cookie ="username = john ;expires= Sun,31 Dec 2021 20:00:00 UTC;path=/";
//读取cookie
//document.cookie用字符串返回所有cookie
let x =document.cookie;
//改变cookie,直接覆盖
document.cookie ="username = john = Sun,31 Dec 2021 20:00:00 UTC";
//删除cookie
document.cookie = "username =;expires =Sun,31 Dec 2017 12:00:00 UTC;path=/";
//定义cookie 路径以确保删除正确的 cookie
//cookie字符串
//document.cookie;活动的是下面的字符串
//cookie1 = value; cookie2 = value;