我可以设置全局变量并在其他文档就绪事件中使用它们吗? [英] Can I set global variables and use them in other document ready events?
问题描述
我使用jQuery 1.6.2和ColdFusion 9。
当请求页面时,包括许多文件。几个文件包含jQuery文档就绪方法。我想设置一些全局变量,我可以在整个页面使用。例如,我想在幻灯片中使用这些变量:
SlideUpRate = 400;
SlideDownRate = SlideUpRate * 2;
这看起来不一致。是否有办法使其始终如一地工作?
+++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++
答案
在index.cfm文件中,我设置我可以在其他jQuery中使用和重用的全局变量。
< script type =text / javascript>
var SlideUpRate = 250;
var SlideDownRate = SlideUpRate * 2;
var HideRate = 250;
var ShowRate = HideRate * 2;
var ImageUnsaved =layout / checkbox_unsaved.png;
var ImageSaved =layout / checkbox_saved.png;
$(document).ready(function(){
//其他jQuery东西
});
是的,您可以将它们添加到全局命名空间:
var globalVar1;
$(document).ready(function(){
globalVar1 =something;
});
$(document).ready(function(){
alert(globalVar1);
});
如果你不想用乘法变量来污染全局范围,你可以让一个对象包含这些变量: / p>
var vars = {};
然后在 ready
到 vars
对象。
$(document).ready (){
vars.my_variable_1 =something;
});
和另一个 ready
函数:
$(document).ready(function(){
alert(vars.my_variable_1);
});
http://jsfiddle.net/aalouv/QDPAm/1/
我不明白为什么你的示例不能工作。也许是因为你在设置之前试图访问一些变量?
var vars = {};
$(document).ready(function(){
alert(vars.my_variable_1); // undefined
});
$(document).ready(function(){
vars.my_variable_1 =something;
});
http://jsfiddle.net/aalouv/QDPAm/3/
同样创建没有var指标的变量,会将变量添加到全局范围,所以你可以访问变量:
window
或者之前没有任何命名空间。$(document).ready(function(){
my_variable_1 =something;
});
$(document).ready(function(){
alert(window.my_variable_1);
alert(my_variable_1);
}
http://jsfiddle.net/aalouv/QDPAm/2/
I am using jQuery 1.6.2 and ColdFusion 9.
When a page is requested, many files are included. Several files contain the jQuery document ready method. I want to set some global variables that I can use throughout the entire page. For example, I want to use these variables for my slides:
SlideUpRate = 400; SlideDownRate = SlideUpRate * 2;
It seems that this works inconsistently. Is there a way to make it work consistently?
+++++++++++++++++++++++++++++++++++++++++++++++ Answer
In the index.cfm file, I set my global variables that can be used and reused in other jQuery throughout the rendered page.
<script type="text/javascript"> var SlideUpRate = 250; var SlideDownRate = SlideUpRate * 2; var HideRate = 250; var ShowRate = HideRate * 2; var ImageUnsaved = "layout/checkbox_unsaved.png"; var ImageSaved = "layout/checkbox_saved.png"; $(document).ready(function() { // other jQuery stuff });
解决方案Yes you can by adding them to the global namespace:
var globalVar1; $(document).ready(function(){ globalVar1 = "something"; }); $(document).ready(function(){ alert(globalVar1); });
If you don't want to pollute the global scope with multiply variables you can make an object to contain these variables:
var vars = {};
And then in your
ready
functions add variables to thevars
object.$(document).ready(function(){ vars.my_variable_1 = "something"; });
And another
ready
function:$(document).ready(function(){ alert(vars.my_variable_1); });
http://jsfiddle.net/aalouv/QDPAm/1/
I cant see why your example shouldn't work. Maybe because your are trying to access some variables before they are set?
var vars = {}; $(document).ready(function(){ alert(vars.my_variable_1); // undefined }); $(document).ready(function(){ vars.my_variable_1 = "something"; });
http://jsfiddle.net/aalouv/QDPAm/3/
Also creating variables without the var indicator first will add the variable to the global scope, So you can access the variable with:
window
or just without any namespace before.$(document).ready(function(){ my_variable_1 = "something"; }); $(document).ready(function(){ alert(window.my_variable_1); alert(my_variable_1); });
http://jsfiddle.net/aalouv/QDPAm/2/
这篇关于我可以设置全局变量并在其他文档就绪事件中使用它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!