【问题标题】:Passing variables between multiple functions in jQuery?在jQuery中的多个函数之间传递变量?
【发布时间】:2014-10-01 22:29:49
【问题描述】:

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

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.

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

编辑:

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

checkmyresolutin() 由jQuery resize function() 运行之后,我只需要以某种方式将enter code here 的内容传递给function_abc()

【问题讨论】:

  • 我有点困惑。现在你不需要传递任何东西;该变量是全局变量。
  • 也可以从resize 处理程序调用function_abc() 吗?
  • my_important_Var_X -vs- my_important_var_X ?
  • @TimVermaelen 错字...对不起
  • @Bergi 不行,必须这样。

标签: javascript jquery pass-by-reference


【解决方案1】:

你有一个错字:

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,带有一个恼人的警报,表明它正在工作。

我还创建了a different version,其中在调整大小时会自动调用 function_abc(),只是为了展示它是如何工作的。

查看 Javascript 中变量范围的 this discussion

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2019-06-28
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多