1.窗口事件
事件名 | 描述 |
---|
onload | 窗体内容被加载 |
onunload | 窗体内容卸载 |
onbeforeunload | 窗体内容卸载前 |
onresize | 窗体大小发生变化 |
onscroll | 窗口内部发生滚动 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body style="height: 1500px">
<div style="height: 300px;width: 300px;border: 1px solid black">
</div>
</body>
<script>
window.onload = function () {
alert("窗体打开");
}
window.onunload = function () {
alert("窗体关闭");
}
window.onbeforeunload = function () {
alert("窗体关闭前");
}
window.onresize = function () {
console.log("高" + window.outerHeight + "宽" + window.outerWidth);
}
window.onscroll = function () {
console.log("你滑动了窗体");
}
</script>
</html>
2.鼠标事件
事件名 | 描述 |
---|
onclick | 鼠标单击事件 |
ondblclick | 鼠标双击事件 |
onmouseover | 鼠标移入事件 |
onmouseout | 鼠标移出事件 |
onmouseenter | 鼠标进入事件 |
onmouseleave | 鼠标离开事件 |
onmousedown | 鼠标按下事件 |
onmouseup | 鼠标抬起 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<input type="button" id="button1" style="width: 150px;height: 60px;background-color: pink" value="按钮">
<input type="button" id="button2" style="width: 150px;height: 60px;background-color: palegoldenrod" value="按钮">
</div>
</body>
<script>
let b1 = document.getElementById("button1");
let b2 = document.getElementById("button2");
b1.onclick = function () {
console.log("你点击了按钮1")
}
b2.ondblclick = function () {
console.log("你双击了按钮2")
}
b1.onmouseover = function () {
console.log("你进入了按钮1")
}
b1.onmouseout = function () {
console.log("你移出了按钮1")
}
b2.onmouseenter = function () {
console.log("你进入了按钮2")
}
b2.onmouseleave = function () {
console.log("你移出了按钮2")
}
b2.onmousedown = function () {
console.log("你在按钮2按下了鼠标")
}
b2.onmouseup = function () {
console.log("你在按钮2松开的鼠标")
}
</script>
</html>
3.键盘事件
事件名 | 描述 |
---|
onkeydown | 按键按下 |
onkeyup | 按键抬起事件 |
onkeypress(淘汰) | 按键按压事件 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<label><input type="text" id="text"></label>
</body>
<script>
let t = document.getElementById("text");
t.onkeydown = function () {
console.log("键盘按下")
}
t.onkeyup = function () {
console.log("键盘抬起")
}
t.onkeypress = function () {
console.log("键盘按下")
}
</script>
</html>
4.表单事件
事件名 | 描述 |
---|
onchange | 内容发生改变事件 |
onselect | 内容选中事件 |
onblur | 失去焦点 |
onfocus | 获得焦点 |
onsubmit | 表单提交事件 |
onreset | 表单重置事件 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="https://www.baidu.com" id="f">
<input type="text" name="text" id="text">
<input type="submit" id="button" value="登录">
<input type="reset" id="button1" value="重置">
</form>
</body>
<script>
document.getElementById('f').addEventListener('submit', function (e) {
let text = this.text.value;
if (text.length < 1) {
e.preventDefault();
alert("请输入值");
}
})
document.getElementById('text').addEventListener('change', function () {
console.log("内容改变了");
})
document.getElementById('text').addEventListener('select', function () {
console.log("文本框中内容被选中啦");
})
document.getElementById('text').addEventListener('focus', function () {
console.log("文本框获得焦点");
})
document.getElementById('text').addEventListener('blur', function () {
console.log("文本框失去焦点");
})
document.getElementById('f').addEventListener('reset', function () {
alert("文本框重置啦");
})
</script>
</html>