js如何设置多种事件

想给test设置多个事件,默认是onclick,如果传参,可以传事件类型,eventType,比如onmouseover,等等,怎么实现,搞了很久还没搞好,麻烦大家帮忙解答一下,谢谢啊

<!DOCTYPE html>

<html>


<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>

</head>


<body>

    <div id="test">

        a

    </div>

    <script>

        var a = 0;

        var test = document.getElementById("test");

        var eventType = onmouseover || onclick;

        test.onclick = function () {

            a++;

            console.log(a);

            a % 2 == 0 ? test.innerHTML = "a" : test.innerHTML = "b";

        }

    </script>

</body>


</html>


慕田峪9158850
浏览 554回答 1
1回答

POPMUISE

addEventListener了解一下,绑定多个事件['click','mouseover','mouseout'].forEach(function(item,index){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;window.addEventListener(item,&nbsp;fun); })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript