为什么同一文档加载时,单独的jQuery文件不能一起工作?

问题描述 投票:0回答:1

我的问题的示例可以与以下三个文件重复...

index.htm:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
</head>
<body>
Hello, world!
</body>
</html>

script1.js:

$(function() {
    const THE_TEXT = 'This is THE_TEXT';
});

script2.js:

$(function() {
    function log_THE_TEXT() {
        console.log(THE_TEXT);
    }

    log_THE_TEXT();
});

加载页面后,我希望在控制台日志中看到“这是THE_TEXT”。相反,会出现以下错误:

ReferenceError:未定义THE_TEXT script2.js:6:3

和:

jQuery。延迟的例外:未定义THE_TEXT log_THE_TEXT @ http://example.com/js-test/script2.js:6:3

如果我在script2.js中定义该常数,它将按预期工作。那为什么它不起作用,有没有办法做到呢?

javascript jquery
1个回答
0
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.