是否可以将一个JavaScript变量的值导出到另一个JavaScript?

elh*_*bre 3 javascript variables jquery export

我已经使用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);}
   });

}); 
Run Code Online (Sandbox Code Playgroud)

Rus*_*Cam 6

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

jQuery $.data()也非常适合根据元素存储数据,这意味着您不需要使用全局变量并污染全局名称空间。

编辑:

根据您的评论,声明变量的方式有所不同,这些变量决定了JavaScript中变量的范围。

全局变量

在函数外部声明变量,例如

var myVariable;
Run Code Online (Sandbox Code Playgroud)

要么

myVariable;
Run Code Online (Sandbox Code Playgroud)

不会有什么不同-两个变量都具有全局范围。实际上,第二种方法即使在函数内部也将给出可变的全局范围。例如

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
}
Run Code Online (Sandbox Code Playgroud)

这种情况下的区别在于,警报将失败,并且globalVariable在执行完secondFunction()之前不会显示的值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);
  }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,一个字符串值"I'm not really hiding"被存储针对使用密钥串中的文档对象myVariablefirstFunction。然后可以从脚本中其他任何位置的缓存对象中检索此值。尝试从缓存对象中读取一个值而不先对其进行设置将产生undefined

请查看此工作演示,以了解更多详细信息。

由于不使用全局变量的原因,请查看本文

  • 是的,@ elhombre您需要考虑范围而不是“文件”。阅读这样的内容应该对您有帮助-http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3 (2认同)