onclick
<body>
<header id="head">我是头部标签header>
body>
<script>
var head = document.getElementById("head")
head.onclick = function () {
console.log("我是鼠标单击事件");
}
script>
ondblclick
onmouseover
onmouseout
onmouseenter
onmouseleave
onmousemove
onmouseup
onmousedown
onfocus
获取里面的值:value
<body>
<input type="text">
body>
<script>
var text = document.getElementsByTagName("input")[0]
text.onfocus = function () {
console.log("鼠标获取焦点了");
}
script>
onblur
onkeydown
<body>
<input type="text">
body>
<script>
var text = document.getElementsByTagName("input")[0]
// 1、按键按下事件onkeydown
text.onkeydown = function () {
console.log("键盘按下了");
}
// 2、按键抬起事件onkeyup
text.onkeyup = function () {
//注意有连接符的要去掉,后面字母要大写
this.style.backgroundColor = "red"
}
script>
onkeyup
window.onload
window.onscroll
<body style="height: 200px;">
<header id="head">我是头部标签header>
<input type="text">
body>
<script>
window.onscroll = function () {
console.log('滚动了')
}
script>
事件源.事件类型 = 事件的处理程序
解绑: 事件源.事件类型 = null
==注意:==普通的事件不能用remove,绑定的也只能用remove
<head>
<style>
.headObj {
height: 100px;
background-color: red;
}
style>
head>
<body>
<header id="headObj" class="headObj">我是头部区域标签header>
body>
<script>
var headObj = document.getElementById('headObj')
//绑定:事件源.事件类型 = 事件的处理程序
headObj.ondblclick = function () {
this.style.backgroundColor = 'rgb(0,255,0)'
}
//解绑:
headObj.ondblclick = null
script>
事件源.addEventListener(事件类型,事件的处理程序)
解绑:事件源.removeEventListener(事件类型,事件处理程序的函数名)
注意:这个里面的事件 不加 on
<script>
//绑定:事件源.addEventListener(事件类型,事件的处理程序)
headObj.addEventListener('mouseover', fn1)
function fn1() {
this.style.height = '200px'
}
//解绑:
headObj.removeEventListener('mouseover', fn1)
script>
浏览器兼容性的事件对象手法
var event = event || window.event
在事件中处理程序中的参数 e (就是event)
console.log(e);
clientX与pageX的区别:
相同点:
clientX
与pageX
都是事件的触发点距离左侧浏览器的横坐标
不同点:
clientX
不包含滚动条卷去的距离
pageX
包含滚动条卷去的距离
onmouseover/onmouseout
支持冒泡的,onmouseenter/onmouseleave
不支持冒泡<body>
<a href="https://www.baidu.com" id="link">百度一下a>
<form action="https://www.qq.com" method="get">
<input type="submit" onclick="submitEvent();" />
form>
body>
<script>
document.getElementById('link').onclick = function () {
alert('单击事件执行了!')
// 阻止默认事件
var event = event || window.event
event.preventDefault()
}
function submitEvent() {
alert('单击表单执行了')
// 阻止默认事件
var event = event || window.event
event.preventDefault()
}
script>
<head>
<style>
.secClass {
height: 100px;
background-color: red;
}
style>
head>
<body>
<section id="secId" class="secClass" name_1="fristName">区间section>
<button id="set">设置属性button>
<button id="remove">删除属性button>
body>
<script>
var secObj = document.getElementById('secId')
/*
getAttribute("属性名") 获取元素指定的属性值
*/
console.log(secObj.getAttribute('id'))
console.log(secObj.getAttribute('class'))
console.log(secObj.getAttribute('name_1'))
/*
setAttribute("属性名","属性值") 设置元素的属性名和属性值
*/
document.getElementById('set').onclick = function () {
secObj.setAttribute('id', 'objId')
secObj.setAttribute('name_1', 'lastName')
secObj.setAttribute('age_1', '12')
}
/*
getAttribute("属性名") 删除元素指定的属性名
*/
document.querySelector('#remove').addEventListener('click', function () {
secObj.removeAttribute('class')
secObj.removeAttribute('name_1')
})
script>