Primero introduzca el método general para agregar eventos por JS. El contenido específico es el siguiente
<! Doctype html> <html> <head> <meta charset = "utf-8"> <title> </title> </head> <body> <p id = "p1"> Test Agregar evento: Firefox usa AddEventListener, IE usa adjunta <br> Haga clic en esta etiqueta P, y 2 eventos emergentes están obligados </p> <script> Función Test1 () {alerta ("Test1"). } function test2 () {alert ("test2"); } // Agregar función de método general de evento AddEvent (Element, E, Fn) {// Firefox usa addEventListener para agregar eventos if (element.addeventListener) {element.addeventListener (e, fn, false); } // es decir, use AttachEvent para agregar eventos más {element.attachevent ("on"+e, fn); }} window.onload = function () {var elemento = document.getElementById ("p1"); addEvent (elemento, "hacer clic", test1); addEvent (elemento, "hacer clic", test2); } </script> </body> </html>Formas comunes de unir eventos JS:
Cómo vincular eventos: BIND FUNCIONES EVENTES CON ATRITIBUTOS DE EVENTO
ventaja:
1. Completa la separación de comportamientos
2. Es conveniente para operar el objeto involucrado, porque la función aparece como un atributo ON ***, puede consultar directamente el objeto involucrado usando esto.
3. Objetos de evento fáciles de leer. Cuando se activa el evento, el sistema pasará automáticamente el objeto del evento a la función del evento, y uno de ellos se aprobará.
<? xml versión = "1.0" encoding = "utf-8"?> < xmlns = "http://www.w3.org/1999/xhtml"> <fead> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> JS BINDING </ticle> <scrypTye = "Text/JavaScript"> Window k = document.getElementById ('k'). onClick = function (event) {var jj = document.getElementById ('jj'); jj.style.top = event.clientx+'px'; jj.style.left = event.clienty+'px'; }} </script> <syle> #k {ancho: 60px; altura: 80px; Color de fondo:#80ffff;} #jj {ancho: 60px; altura: 80px; en segundo plano:#ffff00; z-index: 1000; posición: absoluto;} </ystye> </head> <body> <div it = "k"> </div Id = "jj"> </div> </body> </html>Lo anterior se trata de este artículo, espero que sea útil para el aprendizaje de todos.