Javascript 无法捕获<;上的点击事件;a>;href与主干网';s事件函数

Javascript 无法捕获<;上的点击事件;a>;href与主干网';s事件函数,javascript,backbone.js,javascript-events,event-handling,chaplinjs,Javascript,Backbone.js,Javascript Events,Event Handling,Chaplinjs,您好,我无法在主干中的events对象中捕获onclick事件。我正在rails中使用link_to生成链接。有人能帮我吗 HTML: 从文档中: 模板查看.模板([数据]) 而视图模板不是由直接提供的函数 在主干网中,定义模板函数通常是一种很好的约定 根据你的观点。这样,在渲染视图时,您可以 方便地访问实例数据。例如,使用下划线 模板: 所以模板是一个自定义属性,我们可以使用它来指向模板函数。定义这一点是不够的,您必须通过将数据传递给template函数来创建实际的模板,并将结果HTML附加

您好,我无法在主干中的events对象中捕获onclick事件。我正在rails中使用link_to生成链接。有人能帮我吗 HTML:

从文档中:

模板查看.模板([数据])

而视图模板不是由直接提供的函数 在主干网中,定义模板函数通常是一种很好的约定 根据你的观点。这样,在渲染视图时,您可以 方便地访问实例数据。例如,使用下划线 模板:


所以模板是一个自定义属性,我们可以使用它来指向模板函数。定义这一点是不够的,您必须通过将数据传递给template函数来创建实际的模板,并将结果
HTML
附加到views元素

比如:

 module.exports = View.extend({
  initialize: function(){
    this.render();
  },
  template: Handlebars.compile(template), // where template is the handle bars template
  events: {
   'click #btnSubmitModal': 'saveConsent',
   'click #consent-link' : 'openConsent'
  },
  render: function(){
    this.$el.append(this.template(data)); //where data is the data you wish to pass to handle bars template function
  },
  openConsent: function(event){
    event.preventDefault();
    console.log ("asaasagsgsgs");
    view = new modalView({model: this.model})
  }
 });

事件处理程序的作用域是(委托给)视图的元素。一旦将
HTML
附加到视图的元素中(如上例的
render
方法所示),事件将开始工作。

您应该将整个文件转储到gist或添加到此处。你漏掉太多了。如果我认为你做的其他一切都是对的,这应该是可行的,但不太可能是出了什么问题。我不熟悉你在这里使用module.exports(虽然我通常使用require.js),并且对链接的引用感到困惑,HTML应该从js模板呈现,大多数人不会在那里使用rails帮助程序。这是整个视图吗。。?您将在何处将上述模板附加到此视图中。。?
template
的值是什么?该模板是要在此视图中加载的把手中的整个页面模板。@brent此处的链接仅用于创建具有ID的链接,然后我将在我的主干视图中添加事件处理程序。@Ashish:尝试将主干视图el定义为“#flash messages”
    module.exports = View.extend({

   template: template,
   events: {
    'click #btnSubmitModal': 'saveConsent',
    'click #consent-link' : 'openConsent'
   },

   openConsent: function(event){
    event.preventDefault();
    console.log ("asaasagsgsgs");
    view = new modalView({model: this.model})
   },
var LibraryView = Backbone.View.extend({
    template: _.template(...)
});
 module.exports = View.extend({
  initialize: function(){
    this.render();
  },
  template: Handlebars.compile(template), // where template is the handle bars template
  events: {
   'click #btnSubmitModal': 'saveConsent',
   'click #consent-link' : 'openConsent'
  },
  render: function(){
    this.$el.append(this.template(data)); //where data is the data you wish to pass to handle bars template function
  },
  openConsent: function(event){
    event.preventDefault();
    console.log ("asaasagsgsgs");
    view = new modalView({model: this.model})
  }
 });