【问题标题】:Why 'mouseenter' event does only works for the first element of a spacebar #each iteration?为什么'mouseenter'事件只适用于空格键#each迭代的第一个元素?
【发布时间】:2015-12-20 21:03:51
【问题描述】:

我正在尝试在模板组件中制作画布外菜单。我把this article 作为参考。

我有一个非常常见的设置:

  • 一个容器选项卡,我在其中迭代 items 集合
  • 一个 item 组件,其中有我的画布外菜单

换句话说,我在容器中的 items 光标上有一个空格键迭代:

{{#each items}}
  {{> item}}
{{/each}}

在我的item 组件中,我有一个简单的mouseenter 事件附加到<a> DOM 节点。

Template.item.events({
  "mouseenter  #item_menu": function(event, template){
    console.log("hover detected");
    $(template.find('#item_wrapper')).toggleClass('show-nav')
  },
})

它与第一个 item 组件完美配合,但对于所有其他组件,mouseenter 事件不会被触发。为什么?

【问题讨论】:

  • 不要为您的选择器使用 id,因为它们应该是唯一的,而是使用类。

标签: javascript css meteor mouseenter spacebars


【解决方案1】:

将元素和事件选择器从 #item_menu 更改为类 .item_menu ID 应该是唯一的(在您的 html 中一次)

【讨论】:

  • 它有效,谢谢!我希望id 选择器的唯一性得到尊重,因为每个相似的id 都在不同的模板中。看来我错了。 :)
猜你喜欢
  • 1970-01-01
  • 2012-06-12
  • 2021-01-07
  • 2023-02-26
  • 2014-11-28
  • 1970-01-01
  • 2019-06-01
  • 2011-12-08
  • 2021-06-02
相关资源
最近更新 更多