javascript - meteor 按钮上的切换图标

标签 javascript html button meteor icons

我在 meteor 中创建了一个可扩展的表。按下以展开表格的初始按钮上有一个加号图标。表格展开后,我无法将按钮上的图标更改为减号。基本上,我希望图标根据表格是展开还是折叠在加号和减号之间切换。

我的按钮模板:

<template name="expandButton">
  <button class="btn btn-default btn-xs btn-circle">
    <span id="expand" class="glyphicon glyphicon-plus"></span>
  </button>
</template>

模板在 html 中调用并按预期工作。

我最近的尝试是尝试使用一个事件让图标从加号切换到减号:

Template.expandButton.events({
  'click #expand'(event) {
    event.toggleClass('glyphicon-plus glyphicon-minus');
  }
})

我也尝试了其他几种方法,但没有任何效果。我想知道这是否接近于这样做的一种方式,或者这是否是完全错误的。如果这样做是错误的方法,我应该怎么做呢?

感谢您的帮助。不胜感激。

最佳答案

在 Meteor 中,您必须在正确的位置绑定(bind)事件。您想将点击事件绑定(bind)到按钮。

<template name="expandButton">
    <button class="btn btn-default btn-xs btn-circle" id="expandBtn">
        <span id="expand" class="glyphicon glyphicon-plus"></span>
    </button>
</template>

这是你的事件

Template.expandButton.events({
    'click #expandBtn'(event, temp) {
        temp.$('#expand').toggleClass('glyphicon-plus glyphicon-minus');
    }
})

另请注意,在 meteor 事件中,第一个参数是 event,第二个是 template,因此使用 temp.$ 效率更高解析完整的 dom 即 $(#id)

关于javascript - meteor 按钮上的切换图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195886/

相关文章:

javascript - ionic 应用程序中的 Google Analytics 自定义维度

javascript - Node - 在 For 周期内等待请求响应

javascript - 如何增加 sIFR 文本的抗锯齿/模糊效果?

java - 处理填充问题的 list

Android - 每行调用动态监听器的 ListView 动态按钮

javascript - 如何在 Three.js 中从一系列 3D 点创建 3D 多边形?

javascript - ESLint "code"与可修复的 "whitespace"值

html - 悬停在列表元素上的图像

html - 按钮漂浮在图像旁边的 block 中

jquery - 将背景图像滑动到图像链接后面