global对象 全局对象
<!DOCTYPE html> <html lang="en"head> Meta charset="UTF-8"title>Document</style> body{ width:100%; height; } script> var a="aaa; console.log(window.a); body> html>
window.alert() 弹出提示框
window.confirm() 弹出确认框,确认返回true,取消返回false
window.prompt() 弹出输入框,输入内容返回内容,否则返回null
第一个参数为提示信息,第二个参数为默认信息
} #span background#abcdef colororange window.onloadfunction(){ spandocument.getElementById(span); span2span2 btn1btn1 btn2btn2 btn3btn3); btn1.onclick(){ alert(btn1被点击了哦~); } btn2.onclick(){ text2confirm(确定删除小可爱嘛?); if(text2){ span.style.displaynone; }else{ return; } } btn3.onclick text3prompt(请输入你最喜欢的颜色,仙女粉); span2.innerHTMLtext3; } } span id="span">我是小可爱span><br> 我最喜欢的颜色是:="span2"></button ="btn1">alertbutton="btn2">confirm="btn3">prompt>
window.open() 打开新窗口
第一个参数:页面
第二个参数:页面命名
window.close() 关闭当前窗口
当我加入这段代码想要关闭窗口时,没有成功,而且控制台提示:Scripts may close only the windows that were opened by it.
(){ window.open(new.htmlnewwidth=400,height=400,left=0,top=0,toolbar=no,menubar=no,scrollbars=no,location=no,status=no(){ window.close();//Scripts may close only the windows that were opened by it. } } >打开新窗口试试~>现在关闭新窗口啦>
查看资料得知,除了IE浏览器之外,像谷歌浏览器和火狐浏览器等,都规定window.close()只能用于关闭弹出类窗口
于是,修改用法,将window.open()打开的窗口保存到变量中,使用.close()关闭该窗口
这应该就是正确打开方式了
(){ newWindowwindow.open((){ newWindow.close(); } } >
成功关闭打开的新窗口
javascript是单线程语言,也就是代码按顺序执行,可以通过以下两个方法调整顺序
延迟调用 setTimeout()
取消延迟调用 clearTimeout()
匿名函数 timer1setTimeout(延迟1s后我来啦!); },1)">1000); setTimeout(myFun,1)">2000 myFun(){ alert(延迟2s后我来啦!); } clearTimeout(timer1);取消timer1的延迟调用 } >
间歇调用 setInterval()
clearInterval() 取消间歇调用
myIntervalsetInterval((){ console.log(h); setTimeout((){ clearInterval(myInterval); },1)">10000); } >
10秒倒计时
countcount innercount.innerHTML; count.innerHTMLinner-1; (inner<=){ clearInterval(myInterval); } },1)">="count">10>
用setTimeout() 实现间歇调用,则需要在setTimeout()中调用自身
); myFun(){ ){ setTimeout(myFun,1)">); }{ clearTimeout(firstTimer); } } firstTimersetTimeout(myFun,1)">);首次调用的定时器 >
★★★我是仙女★★★
☆☆☆我是仙女☆☆☆
#text(){ texttext i0; setInterval((i%2==){ text.innerHTML★★★我是仙女★★★{ text.innerHTML☆☆☆我是仙女☆☆☆; } i++; },1)">500) } ="text">☆☆☆我是仙女☆☆☆>