<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title></head><body><pid="p1">5秒后自动跳转到百度...</p></body><script>
t =5
t1 =setInterval(function(){
t -=1
document.getElementById('p1').innerText = t +'秒后自动跳转到百度...'if(t==0){open('https://www.baidu.com')clearInterval(t1)}},1000)</script></html>
04-时间绑定方法
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title></head><body><!-- 点击问候按钮就弹一个弹框 --><buttononclick="alert('hello!')">问候1</button><buttononclick="btn1Action()">问候2</button><buttonid="btn1">按钮1</button><hr><divid="box1"><button>点击1</button><button>点击2</button><button>点击3</button><button>点击4</button><button>点击5</button></div><buttonid="btn2"onclick="">情况1</button><buttonid="btn3"onclick="">情况2</button><hr></body></html><!--
1.事件绑定 - 当某个标签发生了某件事件之后就做什么(xxx发生xxx就干xxx)
2.事件绑定三要素: 事件源、事件、事件驱动程序
3.绑定事件的三种方式:
1)在html代码中直接给事件源对应标签的事件属性赋值
2)在js中给事件源对应的标签对象的事件属性赋值
4.两种不同绑定方式的选择
1)如果是通过js代码创建的标签,需要绑定事件的时候只能用第二种方法
2)第一种绑定方式,事情驱动程序对应的函数中的this是window对象;第二种绑定方式事件驱动程序中的this是事件源
--><script>// function dianji1(){// var title = document.getElementById('d1').innerText// alert('点击了'+title)// }// function dianji2(){// var title = document.getElementById('d2').innerText// alert('点击了'+title)// }var allBtn = document.getElementById('box1').children
var index =0var len = allBtn.length
while(index < len){var btn = allBtn[index]
btn.onclick=function(){var title =this.innerText
alert('点击了'+title)}
index +=1}
document.getElementById('btn2').onclick = btnAction
document.getElementById('btn3').onclick = btnAction
functionbtnAction(){
console.log(this)alert('情况发生了!')}// 给js创建的标签绑定事件var btn2 = document.createElement('button')
btn2.innerText ='按钮2'var body = document.getElementsByTagName('body')[0]
body.appendChild(btn2)
btn2.onclick=function(){alert('下课啦!')}// 1)在html通过标签绑定事件functionbtn1Action(){alert('hello world!')}// 2)在js中通过标签对象绑定事件var btn1 = document.getElementById('btn1')// btn1.onclick = btn1Action
btn1.onclick=function(){alert('hello js!')}</script>