首页 > Web开发 > 详细

js-事件详解以及案例

时间:2020-04-27 10:20:23      阅读:67      评论:0      收藏:0      [点我收藏+]

事件分类

  • 鼠标类

    单击:click

    双击:dblclick

    按下:mousedown

    抬起:mouseup

    移动:mousemove

    进入:mouseover / mouseenter

    离开:mouseout / mouseleave

    右键:contextmenu

  • 键盘类

    按下:keydown

    抬起:keyup

    按下并抬起:keypress

  • 表单类

    获取焦点:focus

    失去焦点:blur

    输入:input

    内容改变:change

    提交事件:submit

    重置事件:reset

  • 浏览器类

    加载:load

    滚动:scroll

    改变大小:resize

  • on绑定,赋值式绑定

   元素.on事件名 = function(){}

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        #box{width: 100px;height: 100px;background: red}
    </style>
</head>
<body>
    <div id="box"></div>
    <input type="text" id="txt">
</body>
<script>
    // 鼠标事件测试

    var box = document.getElementById("box");
    box.onclick = function(){
        console.log("单击事件发生了")
    }
    box.ondblclick = function(){
        console.log("双击事件发生了")
    }
    box.onmousedown = function(){
        console.log("按下事件发生了")
    }
    box.onmouseup = function(){
        console.log("抬起事件发生了")
    }
    box.onmouseover = function(){
        console.log("进入事件发生了")
    }
    box.onmouseout = function(){
        console.log("离开事件发生了")
    }
    box.onmousemove = function(){
        console.log("移动事件发生了")
    }
    box.oncontextmenu = function(){
        console.log("右键事件发生了");
    }

    // 表单事件测试

    var txt = document.getElementById("txt");
    txt.onfocus = function(){
        console.log("获取焦点事件")
    }
    txt.onblur = function(){
        console.log("失去焦点事件")
    }
    txt.oninput = function(){
        console.log("输入事件")
    }
    txt.onchange = function(){
        console.log("改变内容事件")
    }   
</script>
</html>

 如果感觉对自己有帮助,麻烦点一下关注,会一直盒大家分享知识的,谢谢!!!

js-事件详解以及案例

原文:https://www.cnblogs.com/piaoyi1997/p/12784519.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!