属性操作与事件
鼠标事件
onmouseover鼠标移入事件:在鼠标指针移动到元素上时触发。
onmouseout 鼠标移出事件:在鼠标指针移出元素后触发
这个是状态
onmouseenter鼠标进入事件:在鼠标指针进入到元素上时触发。
onmouseleave 鼠标离开事件:在鼠标指针离开元素后触发
这个是动作
鼠标移入事件
box1.onmouseover = function () {
this.style.fontSize = "26px";
this.style.height = "60px";
console.log(111)
}
鼠标移出事件
box1.onmouseout = function () {
this.innerText = "鼠标移出了哈!";
this.style.height = "30px";
this.style.fontSize = "16px";
}
鼠标进入事件
box2.onmouseenter = function () {
this.style.borderRadius = "12px";
this.style.backgroundColor = "blue";
}
鼠标的离开事件
box2.onmouseleave = function () {
this.style.borderRadius = "0";
this.style.backgroundColor = "purple";
}
onfocus获取焦点事件:在鼠标光标获取输入框焦点时触发
onblur失去焦点事件:在鼠标光标失去焦点时触发。
获取焦点事件
user.onfocus = function () {
this.style.border = "3px solid red";
this.style.outline = "0";
}
失去焦点事件
user.onblur = function () {
console.log(this.value);
}
onclick单击事件:在鼠标指针单击时触发
ondblclick双击事件:在鼠标光标双击时触发。
onkeydown:键盘按下
onkeyup:键盘抬起
onload:浏览器加载完成执行
onscroll:滚动浏览器滚动条时触发
事件进阶
执行事件的步骤:获取元素、绑定事件、书写事件驱动程序
监听事件
绑定监听事件 addEventListener("事件的类型",事件的处理程序)
box1.addEventListener("click", myFunc)
function myFunc() {
this.style.backgroundColor = "blue";
}
解绑监听事件removeEventListener("事件的类型",事件的处理程序)
box1.removeEventListener("click", myFunc);
事件对象
任何事件都有内置对象event,事件对象的兼容性写法为
var event = event || window.event;
// 事件的类型
console.log(event.type);
// 元素的ID
console.log(event.target.id);
// 文本的内容
console.log(event.target.innerText);
// 事件的触发点是距离浏览器左侧的横纵坐标
console.log("横坐标:" + event.clientX + "," + "纵坐标:" + event.clientY);
console.log("横坐标:" + event.pageX + "," + "纵坐标:" + event.pageY);
console.log("我单击header!");
元素的坐标
1、clientX与clientY
clientX 事件属性返回当事件被触发时鼠标指针向对于浏览器页面(或客户区)的水平坐标。客户区指的是当前窗口。
clientY 事件属性返回当事件被触发时鼠标指针向对于浏览器页面(客户区)的垂直坐标。 客户区指的是当前窗口。
2、pageX与e.pageY
pageX/Y:相对于文档边缘,包含滚动条距离
clientX/Y:相对于当前页面且不包含滚动条距离
有兼容性问题 从IE9以后才支持,pageY = clientY + 页面滚动出去的距离

浙公网安备 33010602011771号