简体   繁体   English

如何在另一个jquery文件中调用jquery方法

[英]How to invoke jquery method in another jquery file

How to invoke a jquery method which in another jquery method? 如何调用另一个jQuery方法中的一个jQuery方法?
For example: 例如:
In a.js, I define a variable var url = "ssh"; 在a.js中,我定义了一个变量var url = "ssh";
In b.js, I want to use url in a.js, how can I use? 在b.js中,我想在a.js中使用url,该如何使用?

As known, a variable in the global scope should be accessible to all scripts loaded after it is declared. 众所周知,声明后,所有加载的脚本都应可以访问全局范围内的变量。 Include both JavaScript file in one HTML file, place b.js after a.js so that you can access url variable. 将两个JavaScript文件都包含在一个HTML文件中,然后将b.js放在a.js之后,以便您可以访问url变量。 Like this 像这样

<script type="text/javascript" src="a.js"></script>
<script type="text/javascript" src="b.js"></script>

you can directly access that variable in file b.js. 您可以直接在文件b.js中访问该变量。 if you include both file in your html page. 如果您将两个文件都包含在html页面中。 Because variable / method of any file is global. 因为任何文件的变量/方法都是全局的。

if your var url is global and a.js is imported before b.js then you can use your variable url directly 如果您的var url是全局var url ,并且a.jsb.js之前导入,那么您可以直接使用变量url

example inside b.js alert(url) 例如b.js alert(url)

See what are you looking for is absolutely accessible in terms of correct ordering and a global variable. 根据正确的顺序和全局变量,绝对可以找到您要查找的内容。

In file a.js you have a global var like this: 在文件a.js您具有如下所示的全局a.js

var url = "ssh";

Then you can use this var in b.js if a.js is placed before this file or you can say loaded before b.js . 然后,如果将a.js放在此文件之前,则可以在b.js使用此var,或者可以说在b.js之前b.js

So the order of script files would be: 因此,脚本文件的顺序为:

<script src='a.js'></script>
<script src='b.js'></script>

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

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