【问题标题】:Is it possible to export a value of an variable from one javascript to an other?是否可以将变量的值从一个 javascript 导出到另一个?
【发布时间】:2009-08-20 10:47:48
【问题描述】:

我使用 jquery 和 php 制作了一个网页,其中所有文件都以模块化样式使用。现在我有两个必须相互通信的 JavaScript 文件。一个脚本生成一个包含数字的变量 (id_menu_bar)。我希望这个变量被传输到第二个 JavaScript 并在那里使用。

我该怎么做?

这里是脚本

menu_bar.js

$(document).ready(function() {

function wrapper_action(id_menu_bar) {
  $(".wrapper").animate({height: "0px"});
  $("#changer p").click(function() {
    $(".wrapper").animate({height: "300px"});
  });
}

  $("#select_place li").live("click", function() {
  var wrapper_id = $(".wrapper").attr("id");
  var id_place = this.id;

  if (wrapper_id != "place")
    {
    $("#select_level li").remove();
      $("#select_building").load("menu_bar/menu_bar_building.php?placeitem="+id_place, function() {
        $("#select_building li").click(function() {
        var id_building = this.id;

          if (wrapper_id != "building")
            {
            $("#select_level").load("menu_bar/menu_bar_level.php?buildingitem="+id_building, function() {
              $("#select_level li").click(function() {
                var id_level = this.id;
                wrapper_action(id_level);
              });
            });

            }
          else if (wrapper_id == "building")
            {wrapper_action(id_building);}
        });
      });

      }
   else if (wrapper_id == "place")
    {wrapper_action(id_place);}
   });

}); 

【问题讨论】:

    标签: javascript jquery variables export


    【解决方案1】:

    如果变量id_menu_bar 在全局范围内,那么它可以被页面上的另一个脚本使用。

    jQuery 的$.data() 也适用于针对元素存储数据,这意味着您不需要使用全局变量并污染全局命名空间。

    编辑:

    针对您的评论,您声明变量的方式有所不同,这些变量决定了它们在 JavaScript 中的作用域。

    全局变量

    在函数之外声明一个变量,如

    var myVariable;
    

    myVariable;
    

    不会有任何区别 - 两个变量都将具有全局范围。事实上,第二种方法将给出一个变量全局范围,甚至在函数内部。例如

    function firstFunction() {
        // Local scope i.e. scoped to firstFunction
        var localVariable; 
    
        // Global scope i.e. available to all other JavaScript code running
        // in the page
        globalVariable = "I'm not really hiding";
    }
    
    function secondFunction() {
        // I can access globalVariable here but only after
        // firstFunction has been executed
        alert(globalVariable); // alerts I'm not really hiding
    }
    

    这种情况的不同之处在于,在执行secondFunction() 之前,警报将失败并且不会显示globalVariable 的值,直到执行了firstFunction(),因为这是声明变量的位置。如果变量在任何函数之外声明,警报就会成功并显示globalVariable的值

    使用 jQuery.data()

    使用此命令,您可以将数据存储在元素的缓存对象中。我建议查看源代码以了解这是如何实现的,但它非常简洁。考虑

      function firstFunction() {
          $.data(document,"myVariable","I'm not really hiding"); 
          globalVariable = "I'm not hiding";
      }
    
      function secondFunction() {
          // alerts "I'm not really hiding" but only if firstFunction is executed before
          // secondFunction
          alert($.data(document, "myVariable"));
    
          // alerts "I'm not hiding" but only if firstFunction is executed before
          // secondFunction
          alert(globalVariable);
      }
    

    在这种情况下,使用firstFunction 中的键字符串myVariable 针对文档对象存储字符串值"I'm not really hiding"。然后可以从脚本中其他任何位置的缓存对象中检索此值。尝试在没有先设置的情况下从缓存对象中读取值将产生undefined

    查看此Working Demo了解更多详情。

    关于不使用全局变量的原因,请查看article

    【讨论】:

    • 是的-@elhombre 您需要考虑范围而不是“文件”。阅读这样的内容应该对您有所帮助-webdevelopersnotes.com/tutorials/javascript/…
    • @RichardOD 非常感谢您的启发性评论。我认为没有办法绕过制作文件,因为这只会导致一个大文件,这使得编辑单个功能变得困难。现在我知道在每个文件中我都以 '$(document).ready(function()' 开头,所以我已经在一个范围内,无法通过其他文件访问变量。我如何才能做到以一种合乎逻辑的方式启动一个范围 '$(document).ready(function()' 中的所有文件?
    【解决方案2】:

    它必须要有一个 JavaScript 变量吗?

    您能否使用.data() 函数针对相关元素存储信息?

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多