简体   繁体   English

JQuery .on()方法,具有一个选择器的多个事件处理程序

[英]JQuery .on() method with multiple event handlers to one selector

Trying to figure out how to use the Jquery .on() method with a specific selector that has multiple events associated with it. 试图弄清楚如何将Jquery .on()方法与具有多个与之关联的事件的特定选择器一起使用。 I was previously using the .live() method, but not quite sure how to accomplish the same feat with .on(). 我之前使用过.live()方法,但不太确定如何用.on()完​​成同样的壮举。 Please see my code below: 请参阅下面的代码:

$("table.planning_grid td").live({
  mouseenter:function(){
     $(this).parent("tr").find("a.delete").show();
  },
  mouseleave:function(){
     $(this).parent("tr").find("a.delete").hide();        
  },
  click:function(){
    //do something else.
  }
});

I know I can assign the multiple events by calling: 我知道我可以通过调用以下方式分配多个事件:

 $("table.planning_grid td").on({
    mouseenter:function(){  //see above
    },
    mouseleave:function(){ //see above
    }
    click:function(){ //etc
    }
  });

But I believe the proper use of .on() would be like so: 但我相信正确使用.on()会是这样的:

   $("table.planning_grid").on('mouseenter','td',function(){});

Is there a way to accomplish this? 有没有办法实现这个目标? Or what is the best practice here? 或者这里的最佳做法是什么? I tried the code below, but no dice. 我尝试了下面的代码,但没有骰子。

$("table.planning_grid").on('td',{
   mouseenter: function(){ /* event1 */ }, 
   mouseleave: function(){ /* event2 */ },
   click: function(){  /* event3 */ }
 });

Thanks in advance! 提前致谢!

That's the other way around . 那是另一种方式 You should write: 你应该写:

$("table.planning_grid").on({
    mouseenter: function() {
        // Handle mouseenter...
    },
    mouseleave: function() {
        // Handle mouseleave...
    },
    click: function() {
        // Handle click...
    }
}, "td");

Also, if you had multiple event handlers attached to the same selector executing the same function, you could use 此外,如果您有多个事件处理程序连接到执行相同功能的同一个选择器,您可以使用

$('table.planning_grid').on('mouseenter mouseleave', function() {
    //JS Code
});

If you want to use the same function on different events the following code block can be used 如果要在不同事件上使用相同的功能,可以使用以下代码块

$('input').on('keyup blur focus', function () {
   //function block
})

I learned something really useful and fundamental from here . 我从这里学到了一些非常有用和基础的东西

chaining functions is very usefull in this case which works on most jQuery Functions including on function output too. 链接功能,在这种情况下,其中大多数jQuery的职能, 包括功能输出过的作品是非常有用的。

It works because output of most jQuery functions are the input objects sets so you can use them right away and make it shorter and smarter 它的工作原理是因为大多数jQuery函数的输出都是输入对象集,因此您可以立即使用它们,使其更短更智能

function showPhotos() {
    $(this).find("span").slideToggle();
}

$(".photos")
    .on("mouseenter", "li", showPhotos)
    .on("mouseleave", "li", showPhotos);

And you can combine same events/functions in this way: 您可以通过以下方式组合相同的事件/功能:

$("table.planning_grid").on({
    mouseenter: function() {
        // Handle mouseenter...
    },
    mouseleave: function() {
        // Handle mouseleave...
    },
    'click blur paste' : function() {
        // Handle click...
    }
}, "input");

Try with the following code: 尝试使用以下代码:

$("textarea[id^='options_'],input[id^='options_']").on('keyup onmouseout keydown keypress blur change', 
  function() {

  }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM