JavaScriptDOM事件
1. 掌握什么是事件
2. 掌握HTML事件
3. 掌握DOM0级事件
4. 掌握常用的鼠标与键盘事件
5. 掌握this的指向
什么是事件:
事件就是文档或浏览器窗口中发生的一些特定的交互瞬间。js与html的交互就是通过事件来实现的
HTML事件:
HTML事件
DOM0级事件
HTML事件:
<tag 事件=”执行脚本”></tag>
在HTML元素上绑定事件
执行脚本可以是一个函数的调用
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.btn{width:140px;height:30px;line-height: 30px;background:#00f;
color:#fff;font-size:14px;text-align:center;border-radius:5px;
cursor:pointer;margin-top:30px;}
</style>
</head>
<body>
<input type="button" value="弹 出" onclick="alert('我是按钮')">
<!--鼠标划过按钮时调用mouseoverFn的函数-->
<div class="btn" onmouseover="mouseoverFn(this,'#f00')" onmouseout="mouseoutFn(this,'#ff0')">开始</div>
<div class="btn" onmouseover="mouseoverFn(this,'#0f0')" onmouseout="mouseoutFn(this,'#333')">结束</div>
<script>
function mouseoverFn(btn,bgColor){
// 鼠标划过按钮时,按钮的背景变为红色
btn.style.background=bgColor;
}
function mouseoutFn(btn,bgColor){
btn.style.background=bgColor;
}
</script>
</body>
</html>
鼠标点击事件:
onload:页面加载时触发
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
// 页面加载时执行,unload页面卸载
window.onload=function(){
// 获取box
var box=document.getElementById("box");
var clicked=function(){
alert('我被点击了');
}
box.onclick=clicked;
}
</script>
</head>
<body>
<div id="box">这是一个DIV</div>
</body>
</html>
onclick:页面点击是触发
onmouseover:鼠标滑过是触发
onmouseout:鼠标离开时触发
onfocus:获得焦点时触发
onblur:失去焦点时触发
onchange:域的内容改变时发生
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
padding:50px;
}
.left,.tip{
float:left;
}
.left{margin-right:10px;}
.tip{display:none;font-size:14px;}
</style>
<script>
window.onload=function(){
// 获取文本框和提示框
var phone=document.getElementById("phone"),
tip=document.getElementById("tip");
// 给文本框绑定激活的事件
phone.onfocus=function(){
// 让tip显示出来
tip.style.display='block';
}
// 给文本框绑定失去焦点的事件
phone.onblur=function(){
// 获取文本框的值,value用于获取表单元素的值
var phoneVal=this.value;
// 判断手机号码是否是11位的数字
// 如果输入正确,则显示对号图标,否则显示错号图标
if(phoneVal.length==11 && isNaN(phoneVal)==false){
tip.innerHTML='<img src="img/right.png">';
}else{
tip.innerHTML='<img src="img/error.png">';
}
}
}
</script>
</head>
<body>
<div class="box">
<div class="left">
<input type="text" id="phone" placeholder="请输入手机号码">
</div>
<div class="tip" id="tip">
请输入有效的手机号码
</div>
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
// 页面加载
window.onload=init;
// 初始化
function init(){
// 获取下拉菜单
var menu=document.getElementById("menu");
// 给菜单绑定change事件,一般作用域select或checkbox或radio
menu.onchange=function(){
// 获取当前选中的值
//var bgcolor=this.value;
var bgcolor=menu.options[menu.selectedIndex].value;
// 如果bgcolor为空,则下面的脚本将不执行
// if(bgcolor=="")return;
// 设置body的背景色
// 如果bgcolor为空,则将背景色设为白色,否则是选择的颜色
if(bgcolor==""){
document.body.style.background="#fff";
}else{
document.body.style.background=bgcolor;
}
}
}
</script>
</head>
<body>
<div class="box">
请选择您喜欢的背景色:
<select name="" id="menu">
<option value="">请选择</option>
<option value="#f00">红色</option>
<option value="#0f0">绿色</option>
<option value="#00f">蓝色</option>
<option value="#ff0">黄色</option>
<option value="#ccc">灰色</option>
</select>
</div>
</body>
</html>
关于this指向:
在事件触发的函数中,this是对该DOM对象的引用。
DOM0级事件:
1. 通过DOM获取HTML元素
2. (获取HTML元素).事件=执行脚本
语法:ele.事件=执行脚本
功能:在DOM对象上绑定事件
说明:执行脚本可以是一个匿名函数,也可以是一个函数调用(不要写括号)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.lock{width:140px;height:30px;line-height: 30px;background:#00f;
color:#fff;font-size:14px;text-align:center;border-radius:5px;
cursor:pointer;margin-top:30px;}
.unlock{width:140px;height:30px;line-height: 30px;background:#666;
color:#ccc;font-size:14px;text-align:center;border-radius:5px;
cursor:pointer;margin-top:30px;}
</style>
</head>
<body>
<div class="lock" id="btn">锁定</div>
<script>
// 获取按钮
var btn=document.getElementById("btn");
//var btn=document.querySelector(".lock");
function clickBtn(){
alert("我是按钮");
}
// 点击按钮调用clickBtn这个函数
btn.onclick=clickBtn;
/* // 给按钮绑定事件,this是对该DOM元素的引用
btn.onclick=function(){
// 判断如果按钮是锁定,则显示为解锁,变为灰色,否则显示为锁定,变为蓝色
if(this.className=="lock"){
this.className="unlock";
this.innerHTML="解锁";
}else{
this.className="lock";
this.innerHTML="锁定";
}
if(this.innerHTML=="锁定"){
this.className="unlock";
this.innerHTML="解锁";
}else{
this.className="lock";
this.innerHTML="锁定";
}
}*/
</script>
</body>
</html>
不建议使用HTML事件的原因:
1. 多元素绑定相同事件时,效率低
2. 不建议在HTML元素中写JavaScript代码
onfocus事件用于:
input标签type为text、password
textarea标签
鼠标事件补充:
onsubmit:表单中的确认按钮被点击时发生
onmousedown:鼠标按钮在元素上按下时触发
onmousemove:在鼠标指针移动是发生
onmouseup:在元素上松开鼠标按钮时触发
onresize:当调整浏览器窗口的大小时触发
onscroll:拖动滚动时触发
onmousedown和onmouseup构成了onclick事件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body{height:2000px;}
.box{width:200px;height:200px;background:#f00;overflow:auto;}
</style>
</head>
<body>
<div class="box" id="box">
<p>拖动</p>
<p>拖动</p>
<p>拖动</p>
<p>拖动</p>
<p>拖动</p>
<p>拖动</p>
<p>拖动</p>
<p>拖动</p>
<p>拖动</p>
<p>拖动</p>
</div>
<script>
var box=document.getElementById("box");
// 绑定按下的事件
box.onmousedown=function(){
console.log("我被按下了");
}
// 绑定移动的事件
box.onmousemove=function(){
console.log("我被移动了");
}
// 绑定松开的事件
box.onmouseup=function(){
console.log("我被松开了");
}
// 绑定点击的事件
box.onclick=function(){
console.log("我被点击了");
}
// 浏览器窗口尺寸发生改变时
window.onresize=function(){
console.log("我的尺寸被改变了");
}
// 拖动滚动条
window.onscroll=function(){
console.log("我被拖动了");
}
box.onscroll=function(){
console.log("我是DIV的滚动条");
}
</script>
</body>
</html>
键盘事件与keyCode属性:
onkeydown:在用户按下一个键盘按键时发生
onkeypress:在键盘按键被按下并释放一个键时发生
onkeyup:在键盘按键被松开时发生
keyCode:返回onkeydown,onkeypress或onkeyup事件触发的键的值的字符代码,或键的代码
<script type="text/javascript">
//在事件触发的function里,用一个参数接受事件对象
document.onkeypress=function (event) {
console.log(event.keyCode);
}
</script>
实例:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.text span{font-weight:bold;color:#f00;}
em{font-style:normal;}
b{font-weight:normal;}
</style>
</head>
<body>
<div>
<p class="text">
<b id="showcount">您还可以输入</b>
<span id="totalbox"><em id="count">30</em>/30</span>
</p>
<div class="input">
<textarea name="" id="text" cols="70" rows="4"></textarea>
</div>
</div>
<script>
// 获取文本框及其他元素
var text=document.getElementById("text");
var total=30;
var count=document.getElementById("count");
var showcount=document.getElementById("showcount");
var totalbox=document.getElementById("totalbox");
// 绑定键盘事件
document.onkeyup=function(){
// 获取文本框值的长度
var len=text.value.length;
// 计算可输入的剩余字符
var allow=total-len;
var overflow=len-total;
// 如果allow小于0
if(allow<0){
showcount.innerHTML="您已超出";
totalbox.innerHTML=overflow;
totalbox.style.color='#f00';
}else{
showcount.innerHTML='您还可以输入';
totalbox.innerHTML='<em id="count">'+allow+'</em>/30';
}
}
</script>
</body>
</html>