2010-11-29 11 views
17

¿Cómo puedo decir ..jQuery - no el primero

On Click .not.first() div 
alert('Yeah you clicked a div which is not the first one!'); 

Mi código real:

this.$('thumbnails').children().click(function() { 

       $('#video').animate({width: 164, height: 20, top: 475, marginLeft: 262},0) 

     $('.flv').animate({left: 2222, opacity: '0'},0).css('display', 'none') 
     $('.close').animate({opacity: '0'},0) 
       clicked = 0 

      }); 
+0

Se puede publicar un ejemplo de su actual 'manejador click'? –

+0

hecho agregado mi código – Tomkay

Respuesta

47

Hay un selector :gt() (mayor al índice) o .slice(1) (@bobince 's preferencia :), su pregunta traducida literalmente sería:

$("div:gt(0)").click(function() { 
    alert('Yeah you clicked a div which is not the first one!'); 
}); 
//or... 
$("div").slice(1).click(function() { 
    alert('Yeah you clicked a div which is not the first one!'); 
}); 

Para su pregunta actualizada:

$('thumbnails').children(":gt(0)").click(function() { 
    $('#video').css({width: 164, height: 20, top: 475, marginLeft: 262}); 
    $('.flv').css({left: 2222, opacity: '0'}).hide(); 
    $('.close').css({opacity: '0'}); 
    clicked = 0; 
}); 
//or... 
$('thumbnails').children().slice(1).click(function() { 
    $('#video').css({width: 164, height: 20, top: 475, marginLeft: 262}); 
    $('.flv').css({left: 2222, opacity: '0'}).hide(); 
    $('.close').css({opacity: '0'}); 
    clicked = 0; 
}); 

Nota el uso de .css(), si usted quiere hacer cambios en el estilo de animación no instantáneos, utilice este Istead.

+0

¿Por qué usar esto sobre una combinación de no y el primer selector? –

+0

@Justin - ¿Más rápido, menos para escribir, menos objetos creados? :) –

+3

@downvoter - ¿me gustaría comentar? Esto es correcto y más corto ... así que tengo curiosidad. –

5
$("#id").not(':first').click(function(){ 
alert('Not the first'); 
}); 
8
$(div).not(":first").click(function(){ 
    alert("Yeah you clicked a div which is not the first one!); 
}); 

ver :first y .not de la documentación de jQuery.

$('thumbnails').children().not(":first").click(function() { 
    $('#video').animate({width: 164, height: 20, top: 475, marginLeft: 262},0) 
    $('.flv').animate({left: 2222, opacity: '0'},0).css('display', 'none') 
    $('.close').animate({opacity: '0'},0) 
     clicked = 0 
}); 

Sería la respuesta para la actualización de su pregunta.

+0

¿Por qué encender el motor selector * de nuevo * para esto? No es necesario, de hecho es un gran desperdicio hacerlo. –

+0

gracias Dirty - esto funciona bien para mí. – Tomkay

+0

No se preocupe, pero como lo señala Nick a continuación, sería mejor utilizar .css en lugar de .animate si necesita animaciones instantáneas. – dotty

1
$('div').not(':first').click(function() { 
    alert('Yeah you clicked a div which is not the first one!'); 
}); 

Por su pregunta actualización:

$('thumbnails').children().not(':first').click(function() { 

    $('#video').animate({width: 164, height: 20, top: 475, marginLeft: 262},0) 
    $('.flv').animate({left: 2222, opacity: '0'},0).css('display', 'none') 
    $('.close').animate({opacity: '0'},0) 
    clicked = 0 
}); 
Cuestiones relacionadas