事件监听-on...

380 阅读1分钟

最简单的给元素设置事件监听的方法就是设置他们的on...属性,

例如:

       // 给window对象添加onload事件监听、、onload表示页面都加载完毕了
       
        window.onload = function(){
          
        };

鼠标事件监听

时间名事件描述
onclick当鼠标单击某个对象
ondblclick当鼠标双击某个对象
onmousedown当某个鼠标按键在某个对象上被按下
onmouseup当某个鼠标按键在某个对象被上松开
onmousemove当某个鼠标按键在某个对象上被移动
onmouseenter当鼠标进入某个对象(相似事件onmouseover)
onmouseleave当鼠标离开某个对象(相似事件onmouseout)

案例演示

<!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>Document</title>
    <style>
        div {
            width:200px;
            height:200px;
            background-color:#ccc;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        var oBox = document.getElementById('box');
        // 给box这个盒子添加点击事件监听
        oBox.onclick = function() {
            console.log('我是onclick');
        };
        oBox.ondblclick = function() {
            console.log('我是ondblclick');
        };
        oBox.onmousedown = function() {
            console.log('我是onmousedown');
        };   
        oBox.onmouseup = function() {
            console.log('我是onmouseup');
        };
        oBox.onmouseenter = function() {
            console.log('我是onmouseenter');
        };
        oBox.onmouseleave = function() {
            console.log('我是onmouseleave');
        };
        oBox.onmousemove = function() {
            console.log('我是onmousemove');
        };
    </script>
</body>
</html>

键盘事件监听

事件名事件描述
onkeypress当某个键盘的键被按下(系统按钮如箭头键和功能键无法得到识别)
onkeydown当某个键盘的的键被按下(系统按钮可以识别,并且会先于onkeypress发生)
onkeyup当某键盘的键被松开

案例演示

<!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>Document</title>
</head>
<body>
    姓名:
    <input type="text" id = "nameField">
    <script>
        var nameField = document.getElementById('nameField');

        nameField.onkeydown = function () {
            console.log('我是onkeydown');
        };
        nameField.onkeypress = function () {
            console.log('我是onkeypress');
        };
        nameField.onkeyup = function () {
            console.log('我是onkeyup');
        };
    </script>
</body>
</html>

表单事件监听

事件名事件描述
onchange当用户改变域的内容
onfocus当某元素获得焦点(比如tab键或鼠标点击)
onblur当某元素失去焦点
onsubmit当表单被提交
onreset当表单被重置

案例演示

<!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>Document</title>
</head>
<body>
    <form action="" id="myform">
        <p>
            姓名:
            <input type="text" name="nameField">
        </p>
        <p>
            年龄:
            <input type="text" name="ageField">
        </p>
        <p>
            <input type="submit">
            <input type="reset">
        </p>
    </form>
    <script>
        var myform = document.getElementById("myform");
        // 可以直接通过name属性值myform.nameField,打点调用,不用设置id
        var nameField = myform.nameField;
        var ageField = myform.ageField;
        nameField.onchange = function() {
            console.log('你已经修改完姓名');
        };
        nameField.oninput = function() {
            console.log('你正在修改完姓名');
        };
        nameField.onfocus = function() {
            console.log('姓名框得到焦点');
        };
        nameField.onblur = function() {
            console.log('姓名框失去焦点');
        };
        myform.onsubmit = function() {
            alert('表单提交');
        };
        myform.onreset = function() {
            alert('重置表单')
        }
    </script>
</body>
</html>

页面事件监听

事件名事件描述
onload当页面或图像被完成加载
onunload当用户退出页面

案例演示

<!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>Document</title>    
    <script>
        // 给window对象添加onload事件监听、、onload表示页面都加载完毕了
       
        window.onload = function(){
             // 得到盒子1
            var box1 = document.getElementById('box1');
             // 得到盒子2
            var box2 = document.getElementById('box2');
            console.log(box1);
            console.log(box2);
        };
    </script>
</head>
<body>
    <div id="box1">我是盒子1</div>
    <div id="box2">我是盒子2</div>

</body>
</html>