简体   繁体   English

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

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

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");
  }
}

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 selector pairs . 我相信事件映射不支持逗号分隔选择器,因为逗号用于分隔单个事件名称或event selector

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");
  }
}

I solved a similar problem previously, 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");
  }
});

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

相关问题 在 Meteor 中,如何创建通用事件处理程序? - In Meteor how can I create a generic event handler? 如何将jQuery事件处理程序应用于多个缓存选择器? - How to apply a jQuery event handler to multiple cached selectors? 在 Meteor 中,如何创建一个适用于所有模板的通用事件处理程序? - In Meteor, how can I create a single generic event handler that works across all templates? 我已经创建了一个“ onclick”事件处理程序,现在如何为“ Enter”键创建一个事件处理程序? - I've created an “onclick” event handler, now how do I create an event handler for “Enter” key? 如何在没有Session的Meteor中从事件处理程序修改模板实例? - How do I modify a template instance from an event handler in Meteor without Session? 如何在事件处理程序中重新创建“ this”? - How do I re-create “this” inside an event handler? 如何在jQuery中使用多个选择器? - How do I use multiple selectors in jQuery? 我将如何做一个相当于使用伪类查询多个选择器的 vanilla JS? - How would I do a vanilla JS equivalent of querying multiple selectors with pseudoclasses? 如何将此上下文传递给事件处理程序? - How do I pass the this context into an event handler? 如何用 jQuery 替换事件处理程序? - How do I replace an event handler with jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM