繁体   English   中英

在jQuery中的多个函数之间传递变量?

[英]Passing variables between multiple functions in jQuery?

所以我有一些看起来像这样的代码:

var my_important_var_x = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

function checkmyresolutin() {
   // this tests the broswer/window resolution and print to the body [for css purposes]
  var resolution_is = '768px';
  my_important_var_x = resolution_is; // now the var contains the resolution

  return my_important_Var_X;
}

checkmyresolutin(); // for getting the resolutoin on document ready.

$(window).resize(function(){
   checkmyresolutin();  // this is the important part,every time the browser is resized i need to know ,certain function load/unload on certain resolutions.
});

function_abc(); // this is my problem,ill explain bellow,to keep this code block clean.

因此,在调整浏览器大小并返回分辨率/屏幕大小并存储在my_important_var_x变量中后,调用checkmyresolutin()之后,我需要将最新信息从该变量传递给function_abc(); my_important_var_x 功能...有人吗?

编辑:

伙计们,有错别字,但忽略了它们,我没有复制粘贴我的.js,因为这最多需要400行,这只是一个例子 ,“逻辑”在这里很重要。

我只是需要以某种方式传递的内容enter code herefunction_abc()checkmyresolutin()已被jQuery的运行resize function()

您有错字:

var my_important_var_x = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

应该

var my_important_Var_X = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

看到此JSFiddle ,并显示令人讨厌的警报,以显示其工作正常。

我还制作了一个不同的版本 ,其中function_abc()在调整大小时自动调用,只是为了说明其工作原理。

查看有关 Java语言中变量范围的讨论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM