一、event
事件对象,存储当前事件相关的信息。
低版本ie中用event,标准浏览器,事件函数中第一个形参代表事件对象.例:
document.onclick = function(e){
console.log(e);
}
// 兼容ie
document.onclick = function(e){
e = e || window.event
console.log(e);
}
2、冒泡事件
冒泡就是事件发生在子元素身上,却默认一直向上传递,传递到父元素,也会触发父元素。
阻止冒泡方法: e.stopPropagation() 用于事件只触发于子元素,对父元素无影响。
// 阻止右键菜单事件
document.oncontextmenu = function(e){
e.preventDefault()
}
// 阻止f12打开控制台
document.onkeydown = function(e){
if(e.key === "F12"){
e.preventDefault()
}
}