当前位置:网站首页>[summary of two registration methods]

[summary of two registration methods]

2022-07-05 03:33:00 renrenrenrenqq

Summary of two registration methods

on Registration method at the beginning

Register multiple ways for the same event , The bottom will cover the top , Only one , Have uniqueness
Commonly used onclick Click on an object ,onmousemove Mouse moved ,onkeydown A key on a keyboard is pressed

<!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>
    <button id="annniu">button</button>
</body>
<script>
    var button=document.querySelector('#annniu')
 
    button.onclick=function(){
    
        console.dir('1')
    }
   button.onclick=function(){
    
        console.dir('2')
    }
</script>
</html>

Express only 2

2. Monitoring mode

Register multiple ways for the same event , They don't cover each other
utilize addEventListener(type( Event type , No addition on),listener( Event handler ),useCapture( Boolean value , It can be omitted , It is often used to judge bubble and capture ));
true capture ,false Bubbling

<!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>
    <button id="annniu">button</button>
</body>
<script>
    var button=document.querySelector('#annniu')
    button.addEventListener('click',function()
    {
    
        console.dir('1')
    })
    button.addEventListener('click',function()
    {
    
        console.dir('2')
    })
</script>
</html>

expression 1 and 2

原网站

版权声明
本文为[renrenrenrenqq]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140734192611.html