2012-06-02 1 views
10

クイックの質問に取り組んで切り替え、私はこれは.on()&ここで一緒にみんなを

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

それ内部のトグルを持つことができるように取得するように見えるカント?何か案は?私はグーグルを試みたが、運がない。

これは(#imageと#brickを持っている)私は、現在、それは要素の息子、すべてのページに変更を適用しません、.onで動作するように取得しようとして、コード、

$("#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

をあなたはclickイベントを使用する必要があると思うし、次に何かが上のトグルされていますかどうかをテストするifステートメント、または切り替えオフは/トグルありませんトグルイベントでjQueryの.liveを使用する](http://stackoverflow.com/questions/2172614/using-jquery-live-with-toggle-event) – j08691

答えて

24

あなたが直面している問題は、toggleイベントがないことです。 toggle()はjQueryメソッドです。 on()で、toggle()を実装するために、私は、[の可能重複

$('#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:ありがとう!私はそれを理解していなかった(または私はそれがユーザー定義のカスタムイベントとして使用されていると仮定していた)。 =) –

+1

あなたは大歓迎です!あなたの答えをありがとう! :-) – maryisdead

関連する問題