我可以设置全局变量并在其他文档就绪事件中使用它们吗?

发布于 2024-11-30 01:33:54 字数 702 浏览 0 评论 0 原文

我正在使用 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() {

   // other jQuery stuff

});

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

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卖梦商人 2024-12-07 01:33:54

是的,您可以将它们添加到全局命名空间:

var globalVar1;
$(document).ready(function(){
    globalVar1 = "something";
});
$(document).ready(function(){
    alert(globalVar1);
});

http://jsfiddle.net/QDPAm/

如果您不这样做不想用多个变量污染全局范围,您可以创建一个对象来包含这些变量:

var vars = {};

然后在您的“ready”函数中将变量添加到“vars”对象中。

$(document).ready(function(){
    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/

Yes you can by adding them to the global namespace:

var globalVar1;
$(document).ready(function(){
    globalVar1 = "something";
});
$(document).ready(function(){
    alert(globalVar1);
});

http://jsfiddle.net/QDPAm/

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 the vars 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/

书间行客 2024-12-07 01:33:54

Evik,

我会使用对象文字来存储局部变量,如下所示:

  var pageProperties = {
    slideUpRate: 250,
    hideRate: 250,
    imageUnsaved: "layout/checkbox_unsaved.png",
    imageSaved: "layout/checkbox_saved.png",

    getSlideDownRate: function() {
        return slideUpRate * 2;
    },

    showRate : function() {
        return hideRate * 2;
    }
  };

现在,当您想要访问这些页面级属性时,您将使用对象文字:

pageProperties.slideUpRate;
pageProperties.getSlideDownRate();

通过将计算出的费率放入函数中,您将获得的一个好处是您可以封装功能并使这些功能更易于维护和移植。

我要做的另一件事是通过使用更多对象文字来清理多个 $(document).ready 函数,如 Rebecca Murphey 的博客文章中所述: http://blog.rebeccamurphey.com/2009/10/15/using-objects-to-organize-your-code

Evik,

I'd use a object literal to store you local variables like so:

  var pageProperties = {
    slideUpRate: 250,
    hideRate: 250,
    imageUnsaved: "layout/checkbox_unsaved.png",
    imageSaved: "layout/checkbox_saved.png",

    getSlideDownRate: function() {
        return slideUpRate * 2;
    },

    showRate : function() {
        return hideRate * 2;
    }
  };

Now when you want to access these page level properites you'd use the object literal:

pageProperties.slideUpRate;
pageProperties.getSlideDownRate();

One benefit you'll get by putting the calculated rates in a function is that you can encapsulate the functionality and make these functions more maintainable and portable.

Another thing I'd do is clean up the multiple $(document).ready functions by using more object literals as discussed in this blog post by Rebecca Murphey: http://blog.rebeccamurphey.com/2009/10/15/using-objects-to-organize-your-code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文