布局呈现后如何初始化jQuery对象? [英] How to initialize a jQuery object once the layout has rendered?

查看:75
本文介绍了布局呈现后如何初始化jQuery对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一些变量,以便稍后在呈现应用布局之后立即定义jQuery选择器的范围.

I would like to define a few variables that I use to scope my jQuery selectors later on right after my app layout has been rendered.

我试图用以下方式定义它们:

I tried to define them with:

Meteor.startup(function () { /* Define variables */ })

但是它不起作用,对象是空的

But it did not work, the objects were empty

这是我声明应用程序布局的方式:

Here is how I declare my application layout :

Router.configure({
  layoutTemplate: 'layout'
});

这里是布局本身:

<template name="layout">
  <div id="page_container">
    <header id="page_header">
    </header>

    <div id="page_content">
      {{> yield}}
    </div>
  </div>
</template>

以下是我愿意声明的变量:

Here are the variables I am willing to declare :

  L.BODY = $("body");
  L.PC   = $("#page_content", L.BODY);

声明本身可以正常工作,但是L.BODY表明,当我尝试声明这些变量时,我的应用程序主体仍为空.

The declaration itself works fine, but L.BODY show the body of my application is still empty by the time I try to declare those variables.

一旦呈现了应用程序的布局,如何定义jQuery对象?

How can I define jQuery objects once the layout of my application has rendered?

推荐答案

layout只是另一个模板. jQuery需要存在DOM元素才能从中创建对象.

layout is just another template. jQuery needs the DOM element to exist to create an object out of it.

从这里开始 onRendered回调:

From there the onRendered callback is the way to go:

Template.layout.onRendered(function() {
  L.BODY = $('body');
  L.PC   = $('#page_content', L.BODY);
});

这篇关于布局呈现后如何初始化jQuery对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆