我正在構建一個 Django webapp 并想使用一個 JS 擴展來創建每周計劃鏈接。我已將此擴展放在我的專案的靜態目錄中,并在我的 base.html 中匯入了 jQuery 和擴展,如下所示:
<!-- jquery -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js" defer></script>
<!-- jQuery schedule display (includes css and js) -->
<link rel="stylesheet" href="{% static 'Dynamic-Weekly-Scheduler-jQuery-Schedule/dist/jquery.schedule.css' %}">
<script src="{% static 'Dynamic-Weekly-Scheduler-jQuery-Schedule/dist/jquery.schedule.js' %}"></script>
然而,當我嘗試將資料傳遞到這個插件時:
<script type="text/javascript">
console.log({{schedule_data}})
$("schedule").jqs({{schedule_data}})
</script>
我在 jquery.schedule.js 中得到一個錯誤:
Uncaught ReferenceError: jQuery is not defined
指的是這個檔案的結尾是:
$.fn[pluginName] = function (options) {
var ret = false;
var args = Array.prototype.slice.call(arguments);
var loop = this.each(function () {
if (!$.data(this, 'plugin_' pluginName)) {
$.data(this, 'plugin_' pluginName, new Plugin(this, options));
} else if ($.isFunction(Plugin.prototype[options])) {
ret = $.data(this, 'plugin_' pluginName)[options](args);
}
});
if (ret) {
return ret;
}
return loop;
};
})(jQuery, window, document);
我究竟做錯了什么?我不應該使用 jQuery CDN 嗎?請注意,jQuery 未在 jquery.schedule.js 檔案中的其他地方定義。謝謝
uj5u.com熱心網友回復:
defer加載 CDN 時可能需要注意使用。defer告訴瀏覽器等待其他所有內容加載完畢,因此在您的代碼中,<script>底部的標簽將首先開始運行。您應該洗掉deferjQuery CDN中的標簽,而不是defer在加載<script>. 這將確保您的 jQuery 庫在您運行腳本之前已完全加載。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/336958.html
標籤:javascript 查询 姜戈
