jQuery事件委托和把手DOM元素

时间:2015-05-09 01:30:18

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

所以我已经有几个小时出现这个问题了,我已经研究过所有其他答案,并且他们都说它与将事件分配给没有这个问题的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

1 个答案:

答案 0 :(得分:0)

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

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