JavaScript与HTML之间的交互式通过事件实现的
onclick 点击事件
onfocus 获取焦点
onblur 失去焦点
onmouseover //鼠标移到某元素之上
onmouseout //鼠标从某元素移开
onload页面加载时触发
onchange域的内容改变时发生
onsubmit//表单中的确认按钮被点击时发生
//有事件一定有对应一个处理结果,用函数表示
onresize//浏览器的尺寸发生改变
onscroll //窗口滚动
onchange事件支持的标签input,select,textarea
onclick onmouseover onmouseout
<style>
/* div:hover{
background: #333;
} */
</style>
</head>
<body>
<div id="test">hello world</div>
<script>
/* 内容,样式,结构 */
var test = document.getElementById("test");
/*
事件
onclick 点击事件
onmouseover 鼠标移到某元素之上
onmouseout 鼠标从某元素移开
*/
test.onclick = function(){
/* this在事件中指向正在执行事件的当前对象 */
this.style.color = "red"
/* innerHTML */
this.innerHTML = "change"
}
/* 鼠标悬停的事件 */
test.onmouseover = function(){
this.style.backgroundColor = '#333'
}
/* 鼠标移除的事件 */
test.onmouseout = function(){
this.style.backgroundColor = '#fff'
}
</script>
![test.gif](https://cdn.nlark.com/yuque/0/2019/gif/446381/1567238282219-42af3eef-4ee9-445a-98e5-c71b1d093c1c.gif#align=left&display=inline&height=164&name=test.gif&originHeight=164&originWidth=276&size=36779&status=done&width=276)
onfocus onblur
<input type="text" id="input" value="good">
<script>
/* onfocus --获取焦点
onblur --失去焦点
*/
var input = document.getElementById("input");
input.onfocus = function(){
this.style.backgroundColor = "red"
}
input.onblur = function(){
this.style.background = "green"
}
</script>
onload onchange
<input type="text" id="input">
<script>
/*
onload 等DOM树以及图片相关资源加载完毕,再执行函数中的代码
*/
window.onload = function(){
var input = document.getElementById("input");
input.onchange = function(event){
console.log(this.value)
}
}
</script>
onresize
<script>
window.onresize = function(){
/* window.innerWidth 获取窗口的width */
console.log(window.innerWidth)
}
</script>
![test4.gif](https://cdn.nlark.com/yuque/0/2019/gif/446381/1567250263227-a9bf2140-6132-48a8-8196-87f64087be04.gif#align=left&display=inline&height=476&name=test4.gif&originHeight=476&originWidth=657&size=669984&status=done&width=657)
onscroll
<style>
body{
height: 2000px;
}
.nav{
height: 60px;
position: fixed;
background: transparent;
left:0;
top:0;
width:100%;
}
</style>
</head>
<body>
<div class="nav" id="nav">导航</div>
<script>
/* onscroll 滚动事件 */
var nav = document.getElementById("nav")
window.onscroll = function(){
/* 获取滚动条距离顶部的高度 */
var scrollTop = document.documentElement.scrollTop;
/* 当滚动条距离顶部的高度达到300时候完全显示 */
var opacity = scrollTop/300;
if(opacity>1){
opacity = 1
}
nav.style.opacity = opacity;
nav.style.backgroundColor = "red"
}
</script>
![test5.gif](https://cdn.nlark.com/yuque/0/2019/gif/446381/1567250776401-0eab8450-35ad-4181-bb6b-0149db311c90.gif#align=left&display=inline&height=476&name=test5.gif&originHeight=476&originWidth=657&size=82618&status=done&width=657)