我在JS中的代码现在变得非常庞大,所以为了清晰起见,我想将部分外包给其他文件。
在我的主文件中,我使用
jQuery(document).ready(function ($) {
$.getScript("/wp-content/plugins/file2.js",function(){
console.log("loaded 2");
});
$.getScript("/wp-content/plugins/file3.js",function(){
console.log("loaded 3");
});
这很管用。
在文件2和3中,我将jQuery与以下函数结合使用:
function getData(){
$.("#foo").on("click", function(){});
}
所以我得到了错误
$ is undefined
因为我不申报美元。
但如果我把美元包装起来。ready function我无法访问文件2中其他文件使用的函数。
如何在不失去函数的全局范围的情况下单独“声明”美元。还是有更好的方法?
我只在html中包括:
<script src="/wp-content/plugins/file1.js" type="text/javascript"></script>
谢谢你的帮助!