jquery .live中的多个函数

时间:2011-04-07 15:51:15

标签: jquery

我想知道是否可以将多个功能绑定到一个直播事件。

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

5 个答案:

答案 0 :(得分: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();
        });
    }
);

答案 1 :(得分: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();
        });
    }
);

答案 2 :(得分:0)

    $('.block a.submit').live("click","keydown","keyup",
       function(){

我相信那样。

答案 3 :(得分:0)

只需删除第二个function() {

即可

为什么要限制一个匿名函数中的指令数?

答案 4 :(得分: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();
          });
      }
);