2011-04-07 30 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 2011-04-07 15:57:40

+0

@尼爾,不,它不會。 '.live()'方法接受一個綁定到事件的方法。對於'hover'事件,它只會綁定提供給'mouseenter'和'mouseleave'的單一方法。 – 2011-04-07 19:24:04

回答

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正在向多個方法提出一個事件。 – 2011-04-07 15:55:20

+0

啊我明白了,謝謝澄清。 – 2011-04-07 15:56:23

1

沒有,但是你可以在一個

$('.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

合併的方法,只需卸下第二function() {

你爲什麼要限制在一個匿名函數的指令數?

0

是的,但是我不認爲你需要有現場活動在這裏

的現場直播節目時,才需要,如果你注入的DOM元素,並希望現有的 事件就可以自動掛接到新的元素..

在任何情況下,bind和live都可以​​與多個偵聽器一起工作。

$('.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(); 
      }); 
     } 
);