js的鼠标点击事件

鼠标点击事件小结:

onclick:点击事件
ondblclick:双击事件(必然会触发单击事件);
onmouseover:鼠标移入;
onmouseout:鼠标移出;
onmouseenter:鼠标移入;
onmouseleave:鼠标移出;
onmousemove:鼠标在元素中移动;
onmousedown:鼠标按下;
onmouseup:鼠标抬起;
oncontextmenu:鼠标右键事件;

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .bigbox {
            width: 200px;
            height: 200px;
            background-color: pink;
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div class="bigbox" id="bigbox">
        <div class="box" id="box"></div>
    </div>
    <script>
        var btn = document.getElementById("bigbox");
        btn.onclick = function () {
            console.log("onclick");
        }

        // 子元素也会触发事件,多次触发
        btn.onmouseover = function () {
            console.log("over");
        }
        btn.onmouseout = function () {
            console.log("out");
        }

        // 只会触发一次事件,子元素不会触发事件
        btn.onmouseenter = function () {
            console.log("enter");
        }
        btn.onmouseleave = function () {
            console.log("leave");
        }

        // 双击事件
        btn.ondblclick = function () {
            console.log("dblclick");
        }

        // 鼠标在元素中移动
        btn.onmousemove = function () {
            console.log("move");
        }

        // 鼠标按下
        btn.onmousedown = function () {
            console.log("down");
        }

        // 鼠标抬起
        btn.onmouseup = function () {
            console.log("up");
        }

        // 右键菜单
        btn.oncontextmenu= function () {
            console.log("右键");
        }
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值