2010-10-12 28 views
5

Tengo un elemento href y tiene evento onclick en él. Quiero cambiar la función después de algún evento. Intenté usar jquery, pero las funciones antiguas y nuevas se activan. Solo quiero que el nuevo t sea despedido.jquery change onclick evento de href

Mi código es:

<a href='#' id='cvtest' onclick='testMe("One")' >TEST</a> 
after some event i am adding following code: 
$("#cvtest").click(function(){ testMe("Two"); }); 

Cuando hago clic en enlace de "Prueba" I Get 2 alertas "uno" y "dos".

¿Cómo detener el primer evento que se disparó o hay alguna otra solución a este problema?

Respuesta

6

No utilice la propiedad onclick obsoleta. Asigne ambos manejadores de eventos usando jQuery. Entonces es fácil eliminar el que ya no quieres.

// Add the original event handler: 
var originalEventHandler = function() { 
    testMe('One'); 
}; 
$("#cvtest").click(originalEventHandler); 

// Then later remove it and add a new one: 
var newEventHandler = function() { 
    testMe('Two'); 
}; 
$("#cvtest").unbind('click', originalEventHandler).click(newEventHandler); 
+0

THX VoteyDisciple – user367134