🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <input type="button" value="按钮" id="btn"/> <script type="text/javascript"> // 向任何标签添加任何一个事件 function addAnyEventListener(element, event, fn){ if(element.addEventListener){ element.addEventListener(event, fn, false); console.log("A"); }else if (element.attachEvent){ element.attachEvent("on"+event, fn); console.log("B"); }else { element["on"+event] = fn; console.log("C"); } } // 可以为标签移除任何一种事件 function removeAnyEventListener(element, eventType, fn) { if (element.removeEventListener) { element.removeEventListener(eventType, fn, false); } else if (element.detachEvent) { element.detachEvent('on' + eventType, fn); } else { element['on'+eventType] = null; } } var btn = document.getElementById("btn"); addAnyEventListener(btn, "click", function(){ alert("成功绑定onclick事件"); }) removeAnyEventListener(btn, "click", function() {}); </script> </body> </html> ```