三、DOM事件
- JavaScript与HTML之间的交互式通过事件实现的
onclick //点击事件
onfocus
onblur
onmouseover //鼠标移到某元素之上
onmouseout //鼠标从某元素移开
onload页面加载时触发
onchange域的内容改变时发生
onsubmit//表单中的确认按钮被点击时发生
//有事件一定有对应一个处理结果,用函数表示
onresize//浏览器的尺寸发生改变
onscroll //窗口滚动
onchange事件支持的标签input,select,textarea
3-1、鼠标点击、鼠标悬停事件
<div id="test">hello world</div><script>/* 内容,样式,结构 */var test = document.getElementById("test");test.onclick = function(){/* this在事件中指向正在执行事件的当前对象 */this.style.color = "pink"/* innerHTML */this.innerHTML = "change"}// 鼠标悬停的事件test.onmouseover = function(){this.style.backgroundColor = 'gray'}// 鼠标移除的事件test.onmouseout = function(){this.style.backgroundColor = '#fff'}</script>
3-2、点击切换隐藏
点击button按钮,如果div是现实的则隐藏,如果隐藏则显示
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script></head><body><div id="test" style="display: block">hello world</div><button id="btn">切换</button><script>/* var btn = document.getElementById("btn");var test = document.getElementById("test");btn.onclick = function(){var value = test.style.display;if(value == "block"){test.style.display = "none"}else{test.style.display = "block"}} */$("#btn").click(function(){/* is(":visible") 判断一个元素是否显示 */var isShow = $("#test").is(":visible")if(isShow){$("#test").hide(300)}else{$("#test").show(300)}})</script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script></head><body><div id="test">hello world</div><button id="btn">切换</button><script>$("#btn").click(function(){/* toggle() 集成了hide(),show() */$("#test").toggle(300)})</script></body>
3-3、隔行变色
<!-- <style>li:nth-child(odd){color: pink;}li:nth-child(even){color: blue;}</style> --></head><body><!-- 偶数项 字体颜色为粉色 奇数项字体颜色为蓝色 --><ul><li>0</li><li>1</li><li>2</li><li>3</li><li>4</li></ul><script>var lis = document.getElementsByTagName("li")/* 1.对li进行遍历 */for(var i=0;i<lis.length;i++){/* 2.下标值取余%2 为零的情况为偶数 */if(i%2==0){lis[i].style.color="pink"}else{lis[i].style.color="blue"}}</script>
3-4、点击导航 div实现跳转
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script><style>.container>div{height: 300px;margin-top: 50px;}.container>div:nth-child(1){background: pink;}.container>div:nth-child(2){background: rgb(231, 250, 58);}.container>div:nth-child(3){background: rgb(158, 241, 252);}.container>div:nth-child(4){background: rgb(243, 140, 99);}.nav{position: fixed;line-height: 40px;background: lightcoral;top: 0;}</style></head><body><div class="nav"><a href="#wei">魏无羡</a><a href="#lan">蓝忘机</a><a href="#xiao">晓星尘</a><a href="#song">宋子琛</a></div><div class="container"><div id="wei">魏无羡</div><div id="lan">蓝忘机</div><div id="xiao">晓星尘</div><div id="song">宋子琛</div></div><script>/* boj.offset().top 获取元素距离顶部的距离attr(param) 获取元素的属性$('html,body').animate({scrollTop:params)}*/$(".nav a").click(function(){/* 1.获取当前元素对应跳转的元素距离顶部的高度 */var attr = $(this).attr("href");var top = $(attr).offset().top;console.log(attr)console.log(top)/* 2.让窗口滚动对应的高度 */$("html,body").animate({scrollTop:top})return false;})</script>
3-5、onbulr onfocus
onfocus 获取焦点
onblur 失去焦点

<input type="text" id="input" value="good">
<script>
var input = document.getElementById("input");
input.onfocus = function(){
this.style.backgroundColor = "red"
}
input.onblur = function(){
this.style.background = "green"
}
/* 键盘松开的时发生的事件 */
input.onkeyup = function(event){
/* keycode返回一个键盘码 */
if(event.keyCode == 13){
/* 只要是对象的属性,都可以通过点的方式去获取值 */
console.log(this.value)
}
}
</script>
3-6、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>
</body>
<input type="text" id="input">
<script>
/*
*/
window.onload = function(){
var input = document.getElementById("input");
input.onchange = function(event){
console.log(this.value)
}
}
</script>
3-7、onload页面加载时触发
onload 等DOM树以及图片相关资源加载完毕,在执行函数中的代码
<input type="text" id="input"> <script> /* */ window.onload = function(){ var input = document.getElementById("input"); input.onchange = function(event){ console.log(this.value) } } </script>
