如何将变量从一个.js文件传递到另一个.js? [英] How can I pass a variable from one .js file to another .js?

查看:583
本文介绍了如何将变量从一个.js文件传递到另一个.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Symfony应用程序,该应用程序的菜单创建睫毛,并使用.js文件将使用load()方法指向每个菜单选项的ulr加载到div中. .js是一个变量,用于反引用每个已创建的选项卡.在以下示例中可以看到这种想法:

I have a Symfony application with a menu that creates lashes and use a .js file to load into a div the ulr pointed to each menu option using the load () method. This .js is a variable that acts counter to reference each created tab. This idea can be seen in this example:

http://jsfiddle.net/JMMS_85/o7610t24/

$("#tabs-"+tabCounter).load($(this).attr('href'));

加载到div中的URL具有其他链接,因此我创建了另一个.js文件以阻止原始链接到"preventDefault()",并且我必须在较早的div中重新加载这些链接,即显示url相同div中的新链接,因此我必须使用变量计数器上的另一个.js文件来了解实际选择的div是什么.

The URL that is loaded into the div has other links, so I created another .js file to block the original link to "preventDefault ()" and I have to reload these links in it earlier div, that is display the url of the new link in the same div, so I have to use the variable counter another .js file on it to know what is the actual div selected.

file1.js

 $(document).ready(function () {
$("#tabs").tabs();
   var tabCounter = 1;
   var cont =1;

        //Code creating the tabs omitted
 $(".container div a").on("click",function(event) {
    event.preventDefault(); 

        tabTitle = $(this).attr("title"),
        addTab();
        $("#tabs-"+tabCounter).load($(this).attr('href'));
        tabCounter++;
        $("#tabs").tabs({active: $('.ui-tabs-nav li:last').index()});
  });
 });

file2.js

$(document).ready(function () {

       $("a").click( function(event)
        {
        event.preventDefault();            

        $("#tabs-"+tabCounter).load(this.href);
    }); 

我的问题是如何将计数器变量第一个.js文件用于另一个文件.

My problem is how to use the counter variable first .js file to another file.

推荐答案

您可以在函数外部创建变量.

You can create the variable outside of functions.

在这里您可以看到JavaScript Scope的工作方式: http://www.w3schools.com/js /js_scope.asp

Here you can see how JavaScript Scope works: http://www.w3schools.com/js/js_scope.asp

这篇关于如何将变量从一个.js文件传递到另一个.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆