javascript 访问 $(document).ready() & jquery 之外的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7048278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:47:53  来源:igfitidea点击:

accessing variable outside of $(document).ready() & jquery

javascriptjquerydom

提问by kamikaze_pilot

so I have a .js file that is included into my html

所以我有一个包含在我的 html 中的 .js 文件

If I put this inside my .js file,

如果我把它放在我的 .js 文件中,

$(document).ready(function(){    
      var siteRoot = $('.site-root').val();
      alert(siteRoot);
});

the code would alert the value properly, but if I do this:

代码会正确提醒值,但如果我这样做:

var siteRoot = $('.site-root').val();
$(document).ready(function(){
      alert(siteRoot);
});

it would alert undefined instead

它会警告 undefined 而不是

is there a way to have something that's in $(document).ready()access variables outside it since if I put the variable inside $(document).ready()it wouldn't be accessible from other js files

有没有办法$(document).ready()在它的外部访问变量中有一些东西,因为如果我把变量放在里面$(document).ready(),它就不能从其他 js 文件中访问

回答by Pablo Fernandez

You can either make it a global:

您可以将其设为全局:

// this is the same as your example, 
// just wanted to stress that it's a part of the window (global) object
window.siteRoot = $('.site-root').val();
$(document).ready(function(){
      alert(window.siteRoot);
});

Or even better, use some kind of namespace, like this:

或者甚至更好,使用某种命名空间,如下所示:

var MyData = {};
MyData.siteRoot = $('.site-root').val();

$(document).ready(function(){
  alert(MyData.siteRoot);
});

回答by Dave Long

The best way to do this is to basically create an empty global variable or create a namespace to store the variables. Then in the document.ready just add your $('.site-root').val()to that variable.

最好的方法是基本上创建一个空的全局变量或创建一个命名空间来存储变量。然后在 document.ready 中将您的添加$('.site-root').val()到该变量中。

var siteRoot = '';

$(document).ready(function(){    
      siteRoot = $('.site-root').val();
      alert(siteRoot);
});

That way you set the siteRoot variable after you know .site-root exists and it is available globally throughout the application.

这样您就可以在知道 .site-root 存在并且它在整个应用程序中全局可用后设置 siteRoot 变量。

回答by karim79

The variable isavailable from within $(document).ready(since it is a global, but you are probably getting undefinedbecause no value is available for .siteRootbefore the document is ready. Just try this:

变量可以从内$(document).ready(,因为它是一个全球性的,但你很可能得到undefined,因为没有可用的值的.siteRoot文档之前准备就绪。试试这个:

var siteRoot = "blahblah";
$(document).ready(function(){
      alert(siteRoot);
});

Now, if you expect a value to be available for that variable globallyand for immediate usein other parts of your application, you will have to re-work your solution such that other parts of your application also make use of it only when the DOM is ready.

现在,如果您希望一个值可用于全局变量并在应用程序的其他部分立即使用,您将不得不重新设计您的解决方案,以便您的应用程序的其他部分也仅在 DOM准备好了。

回答by Brian Mains

Yet you can share by doing something like this:

但是,您可以通过执行以下操作来共享:

$(document).ready(function(){
    window.siteroot = $('.site-root').val();
});

And throughout your app you could reference it via:

在整个应用程序中,您可以通过以下方式引用它:

if (typeof(window.siteroot) !== "undefined") {
  //do this
}

You could also lazy load it:

你也可以延迟加载它:

window.get_siteRoot = function() {
   if (typeof(window.siteroot) !== "undefined")
      return window.siteroot;

   var val = $('.site-root').val();
   window.siteroot = val;
   return window.siteroot;
}

HTH.

哈。