【问题标题】:"Meteor way" for writing jQuery blur/focus用于编写 jQuery 模糊/焦点的“流星方式”
【发布时间】:2014-07-16 13:40:17
【问题描述】:

我目前有一个 jQuery 函数,我用它来跟踪当前页面是否像这样被聚焦..

$(window).on("blur focus", (e) ->
    prevType = $(this).data("prevType")
    if prevType != e.type
        if e.type == "blur"
            ...
        else if e.type == "focus"
            ...
    $(this).data("prevType", e.type)
)

我想将此代码从原始 jQuery 移到 Template.template.events 哈希中,但不确定如何执行,或者是否可能?

【问题讨论】:

    标签: jquery meteor


    【解决方案1】:

    您将事件绑定到window,而不是模板内的html元素,因此无需使用Template.events

    您可以将此代码移动到任何地方,但某些类型的启动脚本或全局事件将是合适的。

    【讨论】:

      【解决方案2】:

      虽然您不能使用事件哈希将事件直接绑定到窗口,但您可以使用UI.body.events(hash) 绑定正文级事件。

      有关我如何在 Meteor 包中设置窗口级模糊/焦点事件的示例,请参阅 https://github.com/mizzao/meteor-user-status/blob/master/monitor.coffee#L151

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        相关资源
        最近更新 更多