DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<button id="btn">按键button>
<script>
const btn = document.getElementById('btn')
// console.log("获取到的元素",btn) 可以开这一行代码看获取到的元素
btn.onclick = function(){
console.log("按钮被点击")
}
script>
<noscript>您的浏览器不支持js哦noscript>
body>
html>
等价于以下代码
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<button id="btn" onclick="show()">按键button>
<script>
const btn = document.getElementById('btn')
// console.log("获取到的元素",btn) 可以开这一行代码看获取到的元素
function show(){
console.log("按键被点击")
}
script>
<noscript>您的浏览器不支持js哦noscript>
body>
html>
The MouseEvent
interface represents events that occur due to the user interacting with a pointing device (such as a mouse). Common events using this interface include click
, dblclick
, mouseup
, mousedown
.
MouseEvent
derives from UIEvent
, which in turn derives from Event
. Though the MouseEvent.initMouseEvent()
method is kept for backward compatibility, creating of a MouseEvent
object should be done using the MouseEvent()
constructor.
Several more specific events are based on MouseEvent
, including WheelEvent
and DragEvent
.
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<button id="btn">按键button>
<script>
const btn = document.getElementById('btn')
// console.log("获取到的元素",btn) // 可以开这一行代码看获取到的元素
btn.onclick = function(){
console.log("按钮被点击")
}
btn.ondblclick = function(){
console.log("按钮被双击了")
}
script>
<noscript>您的浏览器不支持js哦noscript>
body>
html>
或者这样添加
btn.addEventListener('click',()=>{
console.log("按钮被点击了")
})
btn.addEventListener('dblclick',()=>{
console.log("按钮被双击了")
})
btn.addEventListener('mouseenter',()=>{
console.log("鼠标进入")
})
btn.addEventListener('mouseleave',()=>{
console.log("鼠标离开")
})
btn.onmouseenter = function(){
console.log("鼠标进入")
}
btn.onmouseleave = function(){
console.log("鼠标离开")
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<button id="btn">按键button>
<script>
const btn = document.getElementById('btn')
btn.addEventListener('mouseup',(e)=>{
switch(e.button){
case 0:{
console.log("鼠标左键被点击")
break
}
case 1:{
console.log("1号被点击")
break
}
case 2:{
console.log("鼠标右键被点击")
break
}
}
})
script>
<noscript>您的浏览器不支持js哦noscript>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
head>
<body>
<button id="btn">按键button>
<script>
const btn = document.getElementById('btn')
btn.addEventListener('mouseup',(e)=>{
console.log("mouseup接收到的回调参数",e)
})
script>
<noscript>您的浏览器不支持js哦noscript>
body>
html>
官网