想给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>
1 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
addEventListener了解一下,绑定多个事件
['click','mouseover','mouseout'].forEach(function(item,index){ window.addEventListener(item, fun); })
添加回答
举报
0/150
提交
取消