【问题标题】:jQuery event delegation and handlebars DOM elementsjQuery 事件委托和把手 DOM 元素
【发布时间】:2015-05-09 01:30:17
【问题描述】:

所以我已经遇到这个问题几个小时了,我研究了所有其他答案,他们都说这与将事件分配给尚不存在的 dom 元素有关。奇怪的是:一些有问题的 dom 元素起作用,而另一些则不起作用,而且模式非常随机。如果有人对可能导致此问题的原因有任何想法,将不胜感激。

这是我的js代码:

$(document).on('click', '.user_word_list_item', function(event) {
  event.preventDefault();
  console.log('worked!');
});

这是我的车把模板:

<script id="player-word-template" type="text/x-handlebars-template">
  {{#each user_words}}
  <a class="user_word_list_item" id="user_word{{id}}" href="#">{{name}}</a>
  {{/each}}
</script>

这是我在 github 上的项目的链接 https://github.com/laomatt/wordtris/blob/master/app/assets/javascripts/games.js 第 238 行

和视图文件: https://github.com/laomatt/wordtris/blob/master/app/views/games/show.html.erb

【问题讨论】:

    标签: jquery ruby-on-rails dom handlebars.js event-delegation


    【解决方案1】:

    我解决了这个问题,我必须在 jQuery 绑定中设置和锚定标签。所以它必须是:

     $(document).on('click', 'a.user_word_list_item', function(event)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-18
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      相关资源
      最近更新 更多