首先加载时,JQuery库在另一个脚本中不可用
JQuery library not available in another script when loaded first
我有一个单一的HTML页面与各种JS导入。我正在导入JQuery,然后是我自己的自定义脚本,我想在其中使用JQuery库,尽管我得到'$未定义'异常。
我想,因为我加载JQuery第一,我应该能够引用它在我的外部脚本?
RequireJS会解决这个问题吗?
baseTemplate.html
<body>
<template id="navbar"></template>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="navbarTemplate.js"></script>
</body>
navbarTemplate.js
function isTemplateElementSupported() {
return 'content' in document.createElement('template');
}
if (isTemplateElementSupported()) {
$('#navbar').append("<table><tr><td><b>some test</b></td></tr></table>");
} else {
alert('Please update browser!');
}
尝试使用"defer"选项来正确加载你的javascript。
<body>
<template id="navbar"></template>
<script defer src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script defer src="navbarTemplate.js"></script>
</body>