javascript Meteor JS:如何为多个选择器创建事件处理程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21520798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:05:13  来源:igfitidea点击:

Meteor JS: How to do I create event handler for multiple selectors?

javascripteventsmeteor

提问by Chanpory

I am trying to create the same event handler for multiple elements, but cannot find anywhere in the documentation to do this. In the below example, I am trying to create a click handler for all text handings. This works for h1, but not for the rest.

我正在尝试为多个元素创建相同的事件处理程序,但在文档中找不到任何地方来执行此操作。在下面的示例中,我试图为所有文本处理创建一个单击处理程序。这适用于h1,但不适用于其余部分。

Template.page.events({
  'click h1, h2, h3, h4, h5, h6' : function (e, template) {
    console.log("clicked");
  }
}

回答by sbking

Try this:

试试这个:

Template.page.events({
  'click h1, click h2, click h3, click h4, click h5, click h6' : function (e, template) {
    console.log("clicked");
  }
}

I believe event maps do not support comma separated selectors because commas are used to delimit individual event names or event selectorpairs.

我相信事件映射不支持逗号分隔的选择器,因为逗号用于分隔单个事件名称或事件event selector

回答by Peppe L-G

http://docs.meteor.com/#eventmaps

http://docs.meteor.com/#eventmaps

Template.page.events({
   'click h1, click h2, click h3, click h4, click h5, click h6' : function (e, template) {
    console.log("clicked");
  }
}

回答by FullStack

I solved a similar problempreviously, which is reproduced below for handling multiple events on the entire document with a single handler:

我之前解决了一个类似的问题,下面复制了这个问题,用于使用单个处理程序处理整个文档上的多个事件:

Template.template_name_here.events({
  'keyup, click': function(event) {
    event.preventDefault();
    console.log("KEYUP OR CLICK");
  }
});