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

时间:2021-08-22 06:29:53

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

3 个解决方案

#1


53  

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.

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

#2


2  

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

#3


1  

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

#1


53  

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.

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

#2


2  

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

#3


1  

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