Agregue un oyente único a todos los elementos en una clase
Frecuentes
Visto 850 veces
2
Is it possible via javascript or jQuery to add a unique event listener to all elements of a class so that when an event is triggered is is triggered for only that element and not all elements having the same class name?
Por ejemplo, si tengo el siguiente HTML
<div class"clickable-box"><span style="display: none;">Hi There!</span></div>
<div class"clickable-box"><span style="display: none;">Peek-A-Boo!</span></div>
<div class"clickable-box"><span style="display: none;">Now You See Me!</span></div>
...and the following jQuery
$('.clickable-box').click(function() {
$('.clickable-box span').slideToggle('slow');
});
...the above will toggle the reveal the span for all the divs.
Is there a way to make these trigger individually without having to use a unique id or class for each div?
2 Respuestas
5
que necesita
$('.clickable-box').click(function() {
$(this).find('span').slideToggle('slow');
});
También falta =
después de class
en el div
<div class="clickable-box"><span style="display: none;">Hi There!</span></div>
<div class="clickable-box"><span style="display: none;">Peek-A-Boo!</span></div>
<div class="clickable-box"><span style="display: none;">Now You See Me!</span></div>
Demostración: Violín
Respondido el 10 de Septiembre de 13 a las 00:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript jquery or haz tu propia pregunta.