วิธีการทั่วไปที่แนะนำสำหรับการเพิ่มเหตุการณ์ใน JS
<! doctype html> <html> <head> <meta charset = "utf-8"> <title> </title> </head> <body> <p id = "p1"> ทดสอบเพิ่มเหตุการณ์: firefox ใช้ addeventListener, IE ใช้ attedEvent <br> } function test2 () {alert ("test2"); } // เพิ่มฟังก์ชั่นวิธีการทั่วไปของเหตุการณ์ addevent (องค์ประกอบ, e, fn) {// firefox ใช้ addeventListener เพื่อเพิ่มเหตุการณ์ถ้า (element.addeventListener) {element.addeventListener (e, fn, false); } // ie ใช้ attaintEvent เพื่อเพิ่มเหตุการณ์อื่น {element.attacheVent ("on"+e, fn); }} window.onload = function () {var element = document.getElementById ("p1"); addevent (องค์ประกอบ, "คลิก", test1); addevent (องค์ประกอบ, "คลิก", test2); } </script> </body> </html>วิธีการทั่วไปด้านบนของการเพิ่มเหตุการณ์ใน JS คือเนื้อหาทั้งหมดที่ใช้ร่วมกันโดยตัวแก้ไข ฉันหวังว่ามันจะให้ข้อมูลอ้างอิงและฉันหวังว่าคุณจะสนับสนุน wulin.com มากขึ้น