10-关于DOM的事件操作和常用操作
一、JavaScript的组成
JavaScript基础分为三个部分:
-
ECMAScript:JavaScript的语法标准。包括变量、表达式、运算符、函数、if语句、for语句等。
-
DOM:文档对象模型,操作网页上的元素的API。比如让盒子移动、变色、轮播图等。
-
BOM:浏览器对象模型,操作浏览器部分功能的API。比如让浏览器自动滚动。
二、事件
JS是以事件驱动为核心的一门语言。
事件的三要素
事件的三要素:事件源、事件、事件驱动程序。
比如,我用手去按开关,灯亮了。这件事情里,事件源是:手。事件是:按开关。事件驱动程序是:灯的开和关。
再比如,网页上弹出一个广告,我点击右上角的X,广告就关闭了。这件事情里,事件源是:X。事件是:onclick。事件驱动程序是:广告关闭了。
于是我们可以总结出:谁引发的后续事件,谁就是事件源。
总结如下:
-
事件源:引发后续事件的html标签。
-
事件:js已经定义好了(见下图)。
-
事件驱动程序:对样式和html的操作。也就是DOM。
代码书写步骤如下:(重要)
-
(1)获取事件源:document.getElementById(“box”); //类似与ios语言的 UIButton *adBtn = [UIButton buttonWithType:UIButtonTypeCustom];
-
(2)绑定事件: 事件源box.事件onclick = function(){ 事件驱动程序 };
-
(3)书写事件驱动程序:关于DOM的操作
代码举例:
<body>
<div id="box1"></div>
<script type="text/javascript">
// 1、获取事件源
var div = document.getElementById("box1");
// 2、绑定事件
div.onclick = function () {
// 3、书写事件驱动程序
alert("我是弹出的内容");
}
</script>
</body>
常见事件如下:

下面针对这事件的三要素,进行分别介绍。
1、获取事件源的方式(DOM节点的获取)
获取事件源的常见方式如下:
|
1
2
3
4
5
|
var div1 = document.getElementById("box1"); //方式一:通过id获取单个标签var arr1 = document.getElementsByTagName("div1"); //方式二:通过 标签名 获得 标签数组,所以有svar arr2 = document.getElementsByClassName("hehe"); //方式三:通过 类名 获得 标签数组,所以有s |
2、绑定事件的方式
方式一:直接绑定匿名函数
<div id="box1" ></div>
<script type="text/javascript">
var div1 = document.getElementById("box1");
//绑定事件的第一种方式
div1.onclick = function () {
alert("我是弹出的内容");
}
</script>
方式二:先单独定义函数,再绑定
<div id="box1" ></div>
<script type="text/javascript">
var div1 = document.getElementById("box1");
//绑定事件的第二种方式
div1.onclick = fn; //注意,这里是fn,不是fn()。fn()指的是返回值。
//单独定义函数
function fn() {
alert("我是弹出的内容");
}
</script>
注意上方代码的注释。绑定的时候,是写fn,不是写fn()。fn代表的是整个函数,而fn()代表的是返回值。
方式三:行内绑定
<!--行内绑定--> <div id="box1" onclick="fn()"></div> <script type="text/javascript"> function fn() { alert("我是弹出的内容"); } </script>
注意第一行代码,绑定时,是写的"fn()",不是写的"fn"。因为绑定的这段代码不是写在js代码里的,而是被识别成了字符串。
3、事件驱动程序
我们在上面是拿alert举例,不仅如此,我们还可以操作标签的属性和样式。举例如下:
<style> #box { width: 100px; height: 100px; background-color: pink; cursor: pointer; } </style> </head> <body> <div id="box" ></div> <script type="text/javascript"> var oDiv = document.getElementById("box"); //点击鼠标时,原本粉色的div变大了,背景变红了 oDiv.onclick = function () { oDiv.style.width = "200px"; //属性值要写引号 oDiv.style.height = "200px"; oDiv.style.backgroundColor = "red"; //属性名是backgroundColor,不是background-Color } </script>
上方代码的注意事项:
- 在js里写属性值时,要用引号
- 在js里写属性名时,是
backgroundColor,不是CSS里面的background-Color。记得所有的像css属性的text-*,line-*、backgroun-*等在js中都写成驼峰
onload事件
当页面加载(文本和图片)完毕的时候,触发onload事件。
举例:
<script type="text/javascript"> window.onload = function () { console.log("小马哥"); //等页面加载完毕时,打印字符串 } </script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
//入口函数
// 窗口加载 先是文档加载,图片是后加载
console.log(window.onload);
// 1.必须等待图片资源加载完成之后才执行js脚本代码 异步的(不等待) 同步
window.onload = function() {
console.log(document);
console.log(document.documentElement);
console.log(document.body);
};
console.log(1111);
// 2.有事件覆盖
// window.onload = function() {
// // console.log(111);
// }
</script>
</head>
<body>
<div id="box"></div>>
</body>
</html>
有一点我们要知道:js的加载是和html同步加载的。因此,如果使用元素在定义元素之前,容易报错。这个时候,onload事件就能派上用场了,我们可以把使用元素的代码放在onload里,就能保证这段代码是最后执行。
建议是:整个页面上所有元素加载完毕在执行js内容。所以,window.onload可以预防使用标签在定义标签之前。
常用操作
对象的操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>alex</div>
<script>
var persion={
name:'alex',
age:18,
address:'北京昌平区',
pav:function () {
console.log('这是一个函数');
console.log(this.name);
console.log(this.age)
console.log(this.address)
}
};
persion.pav(); //执行函数
</script>
</body>
</html>
对样式的操作如宽度、高度、背景颜色等
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
height: 100px;
width: 100px;
background-color: green;
}
</style>
</head>
<body>
<div id="div1" class="box"></div>
<p>alex</p>
<script>
//1,获取事件源
var arry = document.getElementById('div1');
//2.事件
arry.onclick=function () {
//3.事件驱动 业务
console.log(arry.style);
arry.style.backgroundColor='red';
arry.style.width='400px';
arry.style.float='left';
}
</script>
</body>
</html>
对标签属性操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
width: 42px;
height: 70px;
/*background: url("./images/icon-slides.png") no-repeat -84px 0;*/
background: url("./images/icon-slides.png") ;
}
</style>
</head>
<body>
<div id="div1" class="box">alex</div>
<img src="./images/购物车.png" width="100" alt="" id="shop">
<script>
var oDiv = document.getElementsByClassName('box')[0];
//鼠标悬停上时
oDiv.onmouseover=function () {
this.style.backgroundPosition='0 0';
};
//鼠标移出时
oDiv.onmouseout =function () {
this.style.backgroundPosition='-84px 0';
};
console.log(oDiv);
var isHover = true;
document.getElementById('shop').onclick=function () {
if(isHover){
// 对标签属性操作
this.src='./images/购物车-hover.png';
this.alt='图片提示';
this.title='图片标题';
this.id='app';
this.className='appImage';
isHover=false;
} else{
this.src='./images/购物车.png';
isHover=true;
}
}
</script>
</body>
</html>
控制元素显示隐藏如:display = none
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.child_div{
height: 100px;
width: 100px;
background-color: green;
}
.active {
display: none;
}
</style>
</head>
<body>
<button>隐藏</button>
<div class="father_div">
<div class="child_div "></div>
</div>
<!-- 1.样式属性 display:none|block
2.通过控制标签属性className 对类型添加 移除
3.DOM创建 销毁 创建销毁
-->
<script>
var child = document.getElementsByClassName('child_div')[0];
document.getElementsByTagName('button')[0].onclick=function () {
// child.className +=' active'
// child.style.display = 'none';
child.className = child.className +' active';
};
// console.log(document.getElementsByTagName('button')[0]);
// console.log(document.getElementsByClassName('child_div')[0])
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.child_div{
height: 100px;
width: 100px;
background-color: green;
}
</style>
</head>
<body>
<button>隐藏</button>
<div class="father_div">
<div class="child_div "></div>
</div>
<script>
var isType = true;
var child = document.getElementsByClassName('child_div')[0];
document.getElementsByTagName('button')[0].onclick=function () {
if(isType){
child.style.display = 'none';
isType=false;
} else{
child.style.display='block';
isType=true;
}
}
</script>
</body>
</html>
对值的操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
.child_div{
height: 100px;
width: 100px;
background-color: red;
}
</style>
<body>
<button>隐藏</button>
<div class="father_div">
<div class="child_div"></div>
</div>
<script>
var child = document.getElementsByClassName('child_div')[0];
document.getElementsByTagName('button')[0].onclick=function () {
child.innerText='中国人';
child.innerHTML='<p>中国最牛<p>';
console.log(this.innerText);
console.log(this.innerHTML);
console.log(child.innerText); //只获取文本
console.log(child.innerHTML); //获取文本和标签
this.innerHTML='<span>了么</span>';
console.log(this.innerHTML); //执行结果:<span>了么</span>
console.log(this.innerText);//执行结果:了么
}
</script>
</body>
</html>
dom的简单操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style></style>
<body>
<button>增加</button>
<div class="father_div" id="box"></div>
<script>
setTimeout(function () {
var father = document.getElementById('box');
//DOM的创建 子元素 节点
var child = document.createElement('div');
child.className='child';
child.style.width='100px';
child.style.height='100px';
child.style.backgroundColor='red';
// 父.appendChild(子)
father.appendChild(child);
},5000); //5秒后自动执行
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style>
.active{
background-color: red;
}
.father_div1{
height: 80px;
width: 80px;
background-color: yellow;
position: relative;
}
.child_div1{
height: 200px;
width: 200px;
background-color: red;
position: absolute;
top: 80px;
}
</style>
<body>
<button>按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
<button>按钮4</button>
<div class="father_div1">alex
<div class="child_div1"></div>
</div>
<script>
var arry = document.getElementsByTagName('button');
for(var i =0;i<arry.length;i++){
console.log(arry[i]);
arry[i].onclick=function () {
for (var j = 0;j<arry.length;j++){
arry[j].className='';
}
this.className='active';
}
}
//鼠标悬浮到上面
document.getElementsByClassName('father_div1')[0].onmouseover=function () {
this.children[0].style.display='none';
// console.log(this.children[0])
};
//鼠标离开上面
document.getElementsByClassName('father_div1')[0].onmouseout=function () {
this.children[0].style.display='block';
}
</script>
</body>
</html>
事件案例
1、京东顶部广告栏关闭
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> *{ padding: 0; margin: 0; } .top-banner{ /*position: relative;*/ background-color: rgb(230, 15, 82); } .top-banner .w{ width: 1190px; position: relative; margin: 0 auto; } .top-banner .banner{ display: block; width: 100%; height: 80px; background: url('./close.jpg') no-repeat center 0; } .top-banner .close{ position: absolute; right: 0; top:0; text-decoration: none; color: white; width: 20px; height: 20px; line-height: 20px; text-align: center; } .hide{ display: none; } </style> </head> <body> <div class="top-banner" id="topBanner"> <div class="w"> <a href="#" class="banner"></a> <a href="#" class="close" id="closeBanner">x</a> </div> </div> <script type="text/javascript"> // /需求:点击案例,隐藏盒子。 //思路:点击a链接,让top-banner这个盒子隐藏起来(加隐藏类名)。 window.onload = function(){ // /1.获取事件源和相关元素 var closeBanner = document.getElementById('closeBanner'); var topBanner = document.getElementById('topBanner'); //2.绑定事件 closeBanner.onclick = function(){ //3.书写事件驱动程序 //类控制 //topBanner.className += ' hide';//保留原类名,添加新类名 //topBanner.className = 'hide'; //替换旧类名 topBanner.style.display = 'none'; } } </script> </body> </html>
2.要求实现效果:当鼠标悬停在img上时,更换为另外一张图片;鼠标离开时,还原为本来的图片。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script> //window.onload页面加载完毕以后再执行此代码 window.onload = function () { //需求:鼠标放到img上,更换为另一张图片,也就是修改路径(src的值)。 //步骤: //1.获取事件源 //2.绑定事件 //3.书写事件驱动程序 //1.获取事件源 var img = document.getElementById("box"); //2.绑定事件(悬停事件:鼠标进入到事件源中立即出发事件) img.onmouseover = function () { //3.书写事件驱动程序(修改src) img.src = "image/jd2.png"; // this.src = "image/jd2.png"; } //1.获取事件源 var img = document.getElementById("box"); //2.绑定事件(悬停事件:鼠标进入到事件源中立即出发事件) img.onmouseout = function () { //3.书写事件驱动程序(修改src) img.src = "image/jd1.png"; } } </script> </head> <body> <img id="box" src="image/jd1.png" style="cursor: pointer;border: 1px solid #ccc;"/> </body> </html>
3、鼠标点击颜色变化案列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div id="box" class="box"></div>
<script>
//需求: 默认盒子是200*200 红色 点击盒子让盒子颜色变成绿色
//1.获取事件源(标签)2.事件 3.事件驱动程序
// 获取DOM的三种方式
//获取事件源
var oDiv = document.getElementById('box');
console.log(oDiv);
var oDiv2 = document.getElementsByClassName('box')[0];
console.log(oDiv2);
// 获取出来是伪数组
var oDiv3 = document.getElementsByTagName('div')[0];
console.log(oDiv3);
// var oDiv4 = document.querySelector('.box');
// console.log(oDiv4);
var isRed = true;
//2.事件
oDiv.onclick = function() {
if (isRed) {
//3.驱动程序
console.log(oDiv.style);
oDiv.style.backgroundColor = 'green';
isRed = false;
}else{
//3.驱动程序
console.log(oDiv.style);
oDiv.style.backgroundColor = 'red';
isRed = true;
}
}
</script>
</body>
</html>


浙公网安备 33010602011771号