【问题标题】:Meteor Load Javascript Function After DOM is LoadedMeteor 加载 DOM 后加载 Javascript 函数
【发布时间】:2016-06-29 00:02:17
【问题描述】:

我正在使用 Bootstrap,其中有一个这样的输入复选框:

<input type="checkbox" id="toggle" name="toggle">

我正在使用Bootstrap Switch 使用它们的功能将其转换为开关:

Meteor.startup(function() {
    $("[name='toggle']").bootstrapSwitch();
});

但是,问题是开关只加载了一半的时间。我认为这是因为在 DOM 中加载复选框之前调用了该函数。我认为将其放入 Meteor 启动功能会修复它,但事实并非如此。如何确保在加载复选框后加载此开关?

我试过Template.body.onRendered,也没有成功。

【问题讨论】:

  • 尝试使用Template.body.onCreated
  • @jswny 您想使用Template.X.onRendered,其中X 是复选框的直接父模板。这应该在所有情况下都有效,除非有条件地渲染元素(在这种情况下需要更多技巧,我会指出一个相关问题)。
  • @chackerian 和 David Weldon 我已经尝试了这两种方法,但都没有成功。

标签: javascript node.js twitter-bootstrap mongodb meteor


【解决方案1】:

考虑一下,上面提到的代码在模板myTemplate

my_template.html

<template name="myTemplate">
    <input type="checkbox" id="toggle" name="toggle"/>
</template>

那么你的my_template.js就会有

my_template.js

if(Meteor.isClient) {
    Template.myTemplate.onRendered(function() {
        $("[name='toggle']").bootstrapSwitch();
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多