在 .php 页面的脚本中初始化一个变量,而不是在 .js 文件中获取它

Initializing a variable in script at .php page and not getting it in .js file

在我的 .php 页面底部

(function ($) {
    $(document).ready(function () {
        var Atest = 'Hi';

        $.getScript(app_consts.VIEW_JS + 'something.js');
    });
})(jQuery);

在我写的 something.js 文件中

(function ($) {
    $(document).ready(function () {
        console.log(Atest);
    });
})(jQuery);

并且在控制台中不打印值。我不知道如何获得它。

更改变量的范围,例如..

<script type="text/javascript">
    var Atest = 'Hi';

    (function ($) {
    $(document).ready(function () {
        //var Atest = 'Hi';

        $.getScript(app_consts.VIEW_JS + 'something.js');
    });
    })(jQuery);
</script>

您可以从 here.

阅读更多关于 Javascript Scope 的信息