2011-04-07 4 views
1

하나의 라이브 이벤트에 여러 함수를 바인딩 할 수 있는지 궁금합니다. 당신이 원하는 무엇을jquery .live의 다중 함수

$('.block a.submit').live("click", 
    function(){ 
     $(this).text('Save').parent().each(function(){ 
      $(".value", this).hide(); 
      $(".edit", this).show(); 
      $(this).find('.edit :first').focus(); //focuses on first form element - less clicks 
      $('thead').show(); 
     }); 
    }, 
    function(){ 
     $(this).text('Edit').parent().each(function(){ 
      $(".edit", this).hide(); 
      $(".value", this).show(); 
      $('thead').hide(); 
     }); 
    } 
); 
+0

라이브 이벤트가 '클릭'이 아니라 '호버'인 경우 유효합니다. – Neal

+0

@Neal, 아니요. '.live()'메소드는 이벤트에 바인드 할 단일 메소드를 허용합니다. 'hover' 이벤트에 대해서 그것은'mouseenter'와'mouseleave' 둘 다에 제공된 단일 메소드를 바인딩 할 것입니다. –

답변

2

메신저 확실하지 않은,하지만 당신이 할 수있는 예, 두에 분할 :

$('.block a.submit').live("click", 
    function(){ 
     $(this).text('Save').parent().each(function(){ 
      $(".value", this).hide(); 
      $(".edit", this).show(); 
      $(this).find('.edit :first').focus(); //focuses on first form element - less clicks 
      $('thead').show(); 
     }); 
    }).live('click', 
    function(){ 
     $(this).text('Edit').parent().each(function(){ 
      $(".edit", this).hide(); 
      $(".value", this).show(); 
      $('thead').hide(); 
     }); 
    } 
); 

또는 당신이 하나의 함수에하고 싶은 두 가지를 넣을 수 있습니다 :

$('.block a.submit').live("click", 
    function(){ 
     $(this).text('Save').parent().each(function(){ 
      $(".value", this).hide(); 
      $(".edit", this).show(); 
      $(this).find('.edit :first').focus(); //focuses on first form element - less clicks 
      $('thead').show(); 
     }); 
     //second thing 
     $(this).text('Edit').parent().each(function(){ 
      $(".edit", this).hide(); 
      $(".value", this).show(); 
      $('thead').hide(); 
     }); 
    } 
); 
0
$('.block a.submit').live("click","keydown","keyup", 
     function(){ 

저는 그렇게 생각합니다.

+0

이것은 잘못된 구문입니다 (*는'.live ('click keydown keyup')'*) 여야합니다.하지만 여러분이 이야기하는 개념은 하나의 메소드를 여러 이벤트에 바인딩하는 것에 관한 것입니다. OP는 여러 가지 방법으로 하나의 이벤트를 요청하고 있습니다. –

+0

아아, 알았어, 고마워. –

1

아니,하지만 당신은 그냥 두 번째 function() {

왜 하나 익명 함수에 지시의 수를 제한 할 것이다 제거 하나

$('.block a.submit').live("click", 
    function(){ 
     $(this).text('Save').parent().each(function(){ 
      $(".value", this).hide(); 
      $(".edit", this).show(); 
      $(this).find('.edit :first').focus(); //focuses on first form element - less clicks 
      $('thead').show(); 
     }); 

     $(this).text('Edit').parent().each(function(){ 
      $(".edit", this).hide(); 
      $(".value", this).show(); 
      $('thead').hide(); 
     }); 
    } 
); 
0

에 당신의 방법을 병합 할 수 있습니다?

0

예하지만 난 당신이 DOM 요소를 삽입하고 기존 이벤트가 자동으로 새 요소에 자신을 훅하려면 여기

라이브 이벤트는 필요 라이브 이벤트를 가질 필요가 있다고 생각하지 않습니다 ..

어떤 경우이든 바인딩과 라이브 모두 여러 수신기에서 작동합니다.

$('.block a.submit').bind("click", 
     function(){ 
      $(this).text('Save').parent().each(function(){ 
       $(".value", this).hide(); 
       $(".edit", this).show(); 
       $(this).find('.edit :first').focus(); //focuses on first form element - less clicks 
       $('thead').show(); 
      }); 
     }).bind("click", 
     function(){ 
      $(this).text('Edit').parent().each(function(){ 
       $(".edit", this).hide(); 
       $(".value", this).show(); 
       $('thead').hide(); 
      }); 
     } 
);