【问题标题】:jquery lightbox is not working with Meteorjquery 灯箱不适用于 Meteor
【发布时间】:2012-08-13 16:54:12
【问题描述】:

灯箱曾经可以工作。但是,当我在流星上尝试它时,它不再工作了。

我先添加jquery依赖,将jquery.lightbox.js和jquery.lightbox.css放到client文件夹中。

$ meteor add jquery
$ ls -l client/js/jquery.lightbox.js
$ ls -l client/css/jquery.lightbox.css

这是使用灯箱的测试模板和脚本。

->test.html

<template name="test">
<a href="xxxxxxxx" class="screenshot">
    <img src="xxxxxx" alt="Screenshot" class="thumbnail"/>
    <span class="screenshot-zoom"></span>
</a>
</template>

->screenshot.js

$(function () {
     $(".screenshot").lightbox();
});

如果我将它用作常规 html,它就可以工作。但它不在流星之下。我错过了什么吗?

【问题讨论】:

  • 如果在screenshot.js中调用jQuery函数时没有加载模板,jQuery将看不到模板中的元素。我建议在加载模板时调用该函数。
  • 我是流星新手。你能给我一个如何实现的例子吗?谢谢。
  • 据我所知,模板目前没有onload 事件。因此,您可以重写应用程序逻辑,或使用此处描述的 hacky 解决方案:stackoverflow.com/questions/11167390/…。希望对您有所帮助。
  • 谢谢。我试过了。它仍然不适用于灯箱。 Meteor 的灯箱本身可能存在一些错误。

标签: javascript lightbox meteor handlebars.js


【解决方案1】:

从 Meteor 0.4.0 开始,您可以使用 Template.myTemplate.rendered 方法来执行此操作:

在你的情况下

Template.test.rendered = function() {
    if(!this._rendered) {
      this._rendered = true;
      $(this.find(".screenshot")).lightbox();
    }
}

【讨论】:

    【解决方案2】:

    这是我从 Meteor 的开发人员那里得到的,但是,我没有在新的流星 0.4.0 上验证它。

    http://docs.meteor.com/#meteor_startup

    Meteor.startup(function () (
      $(".screenshot").lightbox();
    });
    

    【讨论】:

    • 这在启动后模板发生变化时不起作用。
    猜你喜欢
    • 1970-01-01
    • 2014-07-14
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    相关资源
    最近更新 更多