1。html事件:
<script>
function show() {
alert('hello');
}
</script>
</head>
<body>
<button id="btn" οnclick="show()">按钮</button>
</body>
2,Dom0级事件
<button id="btn">按钮</button>
<script>
var btn = document.getElementById('btn');
btn.onclick = function() {
// dom 0级事件处理
alert('hello');
}
btn.onclick = function() {
// dom 0级事件只允许绑定一个事件处理函数
alert('ok');
}
btn.onclick = null; //移除事件
</script>
3.DOM2级事件
<script>
window.onload = function() {
var btn = document.getElementById('btn');
btn.addEventListener('click', function() {
alert('ok');
});
function show() {
// dom2级事件允许给元素绑定多个事件处理函数
alert('show');
}
btn.addEventListener('click', show);
//btn.removeEventListener('click', show); //移除事件
btn.addEventListener('mouseover', function() {
console.log('test');
});
}
</script>
4.一次性事件
<script>
var btn = document.querySelector('#btn');
// function show() {
// alert('show');
// btn.removeEventListener('click', show);
// }
btn.addEventListener('click', function() {
alert('show');
// this.disabled = true;
btn.removeEventListener('click', arguments.callee);
// arguments.callee 指向当前函数,。当一个函数必须调用自身的时候,避免使用 arguments.callee(),
});
</script>
5.ie事件处理
<button id="btn">按钮</button>
<script>
var btn = document.getElementById('btn');
function fn() {
alert(1);
}
btn.attachEvent('onclick', fn);
btn.detachEvent('onclick', fn);
</script>
6,事件处理兼容
<script>
function addEvent(elm, type, callback) {
if (elm.addEventListener) {
elm.addEventListener(type, callback);
} else if (elm.attachEvent) {
elm.attachEvent('on' + type, callback);
} else {
elm[on + "type"] = callback;
}
}
window.onload = function() {
var btn = document.getElementById('btn');
addEvent(btn, 'click', function() {
alert(123);
});
}
</script>
7.事件冒泡
<body>
<div id="box">
<button id="btn">按钮</button>
</div>
</body>
<script>
var btn = document.getElementById('btn');
var box = document.getElementById('box');
var body = document.body;
box.addEventListener('click', function(event) {
console.log('div click');
});
body.addEventListener('click', function() {
console.log('body click');
});
btn.addEventListener('click', function(event) {
console.log('btn click');
event.stopPropagation();
});
</script>
7.事件冒泡
<body>
<div id="box">
<button id="btn">按钮</button>
</div>
</body>
<script>
var btn = document.getElementById('btn');
var box = document.getElementById('box');
var body = document.body;
box.addEventListener('click', function(event) {
console.log('div click');
});
body.addEventListener('click', function() {
console.log('body click');
});
btn.addEventListener('click', function(event) {
console.log('btn click');
event.stopPropagation();
});
</script>
8兼容问题1
<body>
<div id="box">
<button id="btn">按钮</button>
</div>
</body>
<script>
var btn = document.getElementById('btn');
var box = document.getElementById('box');
var body = document.body;
box.addEventListener('click', function(event) {
console.log('div click');
});
body.addEventListener('click', function() {
console.log('body click');
});
btn.addEventListener('click', function(e) {
var e = e || event; //兼容 ie只能识别event
console.log('btn click');
if (e.stopPropagation) {
e.stopPropagation();//该方法将停止事件的传播,阻止它被分派到其他 Document 节点。在事件传播的任何阶段都可以调用它。注意,虽然该方法不能阻止同一个 Document 节点上的其他事件句柄被调用,但是它可以阻止把事件分派到其他节点。
} else {
e.cancelBubble = true;
}
});
</script>
9兼容问题2
<script>
// 将事件处理函数通过冒泡托管给父级元素 -- 事件委托
// 应用场景 --- 为未来元素绑定事件
window.onload = function() {
var box = document.getElementById('box');
var btn = document.getElementById('btn');
box.addEventListener('click', function(e) {
var e = e || event;
// console.log(this === e.target);
e.stopPropagation();
console.log('点击了');
});
}
</script>
10事件委托2
<div id="box">
<button>按钮</button>
</div>
<script>
var box = document.getElementById('box');
box.addEventListener('click', function(e) {
var e = e || event;
e.stopPropagation();
if (e.target.nodeName == "BUTTON") {
//给未来元素(脚本动态创建的元素)绑定事件
var button = document.createElement('button');
button.innerHTML = 'new button';
box.appendChild(button);
}
});
</script>
11.事件委派
<body>
<div id="box">
<button id="btn1">第1个按钮</button>
<button id="btn2">第2个按钮</button>
<button id="btn3">第3个按钮</button>
<button id="btn4">第4个按钮</button>
<button id="btn5">第5个按钮</button>
</div>
<script>
var box = document.getElementById('box');
box.addEventListener('click', function(e) {
var e = e || event;
e.stopPropagation();
switch (e.target.id) {
case 'btn1':
console.log(11111);
break;
case 'btn2':
console.log(222222);
break;
case 'btn3':
console.log(33333333333);
break;
case 'btn4':
console.log(44444444444);
break;
case 'btn5':
console.log(5555555555);
break;
}
});
</script>
</body>
12.event对象
<script>
window.onload = function() {
var box = document.getElementById('box');
console.log(box.scrollTop);
document.addEventListener('click', function(e) {
console.log(e.pageX, e.pageY);
console.log("clientX:" + e.clientX + "," + (e.clientY + document.documentElement.scrollTop));
});
box.addEventListener('click', function(e) {
var e = e || event;
// console.log("x:" + e.offsetX + " y:" + e.offsetY); //从当前元素开始计算的XY坐标//从box开始计算鼠标在box中的位置
// console.log("x:" + e.pageX + " y:" + e.pageY); //从页面左上角开始计算的XY坐标//从页面(要累积划到上面的)
// console.log("x:" + e.screenX + " y:" + e.screenY); //从显示屏左上角开始计算的XY坐标
// console.log("x:" + e.clientX + " y:" + e.clientY); //从页面左上角开始计算的XY坐标(不累计)
});
}
</script>
</head>
<body>
<div id="box" style="height:500px;width:500px;background:#ccc;margin:0 auto;"></div>
</body>
12.鼠标事件JavaScript中判断鼠标按键(event.button)
<script>
window.onload = function() {
var box = document.getElementById('box');
box.addEventListener('dblclick', function(e) {
console.log(e.button);
console.log('doubleclick');
});
box.addEventListener('contextmenu', function(e) {
e.preventDefault(); //阻止事件的默认行为
console.log(e.button);
// return false;
});
//
mouseover事件:不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件。
mouseenter事件:只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件。
mouseout事件:不论鼠标指针离开被选元素还是任何子元素,都会触发 mouseout 事件。
mouseleave事件:只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。
var a = document.getElementById('baidu');
a.addEventListener('click', function(e) {
e.preventDefault();
alert('123');
// return false;
});
// click dblclick contextmenu
// mouseover mouseout mousemove mouseenter mouseleave
box.addEventListener('mouseover', function() {
console.log('mouseover');
});
box.addEventListener('mouseout', function() {
console.log('mouseout');
});
box.addEventListener('mouseenter', function() {
console.log('mouseenter');
});
box.addEventListener('mouseleave', function() {
console.log('mouseleave');
});
box.addEventListener('mousemove', function(e) {
console.log(e.pageX, e.pageY);
});
}
</script>
</head>
<body>
<a href="https://www.baidu.com" id="baidu">百度</a>
<div id="box" style="height:500px;width:500px;background:#ccc;margin:0 auto;"></div>
</body>
13.图片跟随
<title>Document</title>
<style>
img {
position: absolute;
}
</style>
</head>
<body>
<img src="timg.jpg" alt="" style="width:120px" id="img">
<script>
var img = document.getElementById('img');
document.addEventListener('mousemove', function(e) {
var e = e || event;
var x = e.pageX;
var y = e.pageY;
img.style.left = x + "px";
img.style.top = y + "px";
});
</script>
</body>
14.键盘事件
<script>
function showEvent(ev) {
for (var i in ev) {
console.log(i + ":" + ev[i]);
}
}
document.addEventListener('keyup', function(e) {
// e.preventDefault();
// showEvent(e);
if (e.altKey) {
switch (e.key) {
case 'b':
location.href = "https://www.baidu.com";
break;
case 's':
location.href = "https://www.sina.com.cn";
break;
}
}
// return false;
});
</script>
</head>
<body>
</body>
15.就绪事件
body>
<img src="timg.jpg" alt="">
<script>
// var img = document.querySelector('img');
// img.addEventListener('load', function() {
// alert('图片加载完毕');
// });
// window.onload = function() {
// alert('onload');
// }
// document.addEventListener('DOMContentLoaded', function() {
// // DOMCountentLoaded 当DOM结构加载完毕执行
// alert('加载完毕');
// });
function $(callback) {
document.addEventListener('DOMContentLoaded', callback);
}
$(function() {
console.log('加载完毕');
});
</script>
16考试题
<script>
// var arr = [1, 2, 3, 4];
// arr.splice(1, 0, [1, 2, 3, 4], 5);
// console.log(arr.length);
// var arr = [1, 2, 3, 4];
// arr.slice(2);
// console.log(arr);
// var arr = [100, 4, 2, 3, 200];
// console.log(arr.sort(function(a, b) {
// return b - a;
// }))
// var arr = [1, 2, 3, 'hello'];
// arr.concat('world');
// console.log(arr.toString());
var arr = new Array(5);
arr[1] = 1;
arr[5] = 2;
console.log(arr.length);
</script>
</head>