2012-06-02 14 views
10

domanda veloce qui ragazzi, I cant sembrano ottenere questo.on() e alternare lavorare insieme

$('#wrap').on('toggle', '#image', function(){ 
    <!-- Do stuff --> 
    }); 

per essere in grado di avere un interruttore al suo interno? Qualche idea? Ho provato su Google, ma senza fortuna.

Questo è il codice che sto cercando di mettersi al lavoro con .on, come attualmente non si applica le modifiche a tutti del figlio elemento di pagina, (che hanno #image e #brick)

$("#result_options").toggle(function() { 
     var image_width = $('#image').width()/2; 
     var brick_width = $('#brick').width()/2; 
     $("#image").css("width",image_width); 
     $("#image").css("padding","4px"); 
     $("#brick").css("width",brick_width); 
    },function(){ 
     $("#image").css("width","300"); 
     $("#image").css("padding","8px"); 
     $("#brick").css("width","314"); 
     $(this).html("Smaller Results"); 
    }); 
}); 
+1

possibile duplicato di [ Usando jQuery .live con l'evento toggle] (http://stackoverflow.com/questions/2172614/using-jquery-live-with-toggle-event) – j08691

risposta

24

Il problema riscontrato è che non è presente l'evento toggle; toggle() è un metodo jQuery. Per implementare un toggle(), con on() penso che avresti bisogno di utilizzare un evento click, e poi un if dichiarazione per verificare se qualcosa è stato attivata, o commutato-off/non-attivata

$('#wrap').on('click', '#image', function(){ 
    if (!$(this).attr('data-toggled') || $(this).attr('data-toggled') == 'off'){ 
     /* currently it's not been toggled, or it's been toggled to the 'off' state, 
      so now toggle to the 'on' state: */ 
      $(this).attr('data-toggled','on'); 
      // and do something... 
    } 
    else if ($(this).attr('data-toggled') == 'on'){ 
     /* currently it has been toggled, and toggled to the 'on' state, 
      so now turn off: */ 
      $(this).attr('data-toggled','off'); 
      // and do, or undo, something... 
    } 
}); 
+0

@maryisdead: grazie! Non me ne ero reso conto (o pensavo che fosse usato come un evento personalizzato definito dall'utente). =) –

+1

Prego! La ringrazio per la risposta! :-) – maryisdead

Problemi correlati