如何在全球范围内提供$(document).ready()函数? [英] How to make $(document).ready() functions available globally?

查看:85
本文介绍了如何在全球范围内提供$(document).ready()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个有趣的问题可能听起来很傻,但是这里有。使用JQuery的ready函数我定义了一些函数,如下所示:

I have an interesting question here that may sound pretty silly, but here goes. Using JQuery's ready function I have defined some functions like so:

$(function(){

  var function1 = function(data){
    //do something
  }

  var function2 = function(data){
    //do something else
  }
});

出于某种原因,为了让IE呈现我正确使用的东西,必须在$(document).ready()函数。但是,一旦我从服务器端获得数据集,我需要触发这些功能。所以我想我会做这样的事情......

For some reason, in order for IE to render what I am using correctly, it must be done in the $(document).ready() function. However, I need to trigger these functions once I have a dataset from the server-side. So I thought I would do something like this...

Object.Namespace.callFunction = function(data){
 function1(data);
}

...要放在脚本中的ready函数之外,以便我可以直接调用它。

...to be placed outside the ready function in a script so that I could call it directly.

不幸的是,我知道这不起作用,因为它看起来不合逻辑,我已经尝试过了!我使所有这些功能都是随意的,因为它与内容无关,而是概念。我还尝试使用事件处理程序在获取数据后触发函数 - 无济于事!在$(document).ready()全局中创建函数的最佳方法是什么?

Unfortunately, I know this doesn't work because well, it does not seem logical and I have tried it!. I made all these functions arbitrary because it does not matter the content, but rather the concept. I have also tried using event handlers to trigger the function once I get that data -- to no avail! What is the best way to make functions inside the $(document).ready() global?

推荐答案

如果你(对于文体理由)想要用$(document).ready内联编写函数,你可以这样做:

If you (for stylistic reasons) want to write the function inline with your $(document).ready, You can do it this way:

var app={}; /*Use whatever your apps name is, abbreviated (something short)*/
$(function()
{
  app.function1 = function(data) { };
  app.function2 = function(data) { };
  // now you can call all functions inside and outside this ready function with the app. prefix
  // if you also want a local reference to the function without the app. prefix, you can do:
  var function1 = app.function1 = function(data) { };
});

这篇关于如何在全球范围内提供$(document).ready()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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