[英]How to invoke a function in javascript which has been declared in a html file
我有一個HTML文件,其中在腳本標簽中聲明了函數displayJsonWithAjax
和函數displayOtherJsonWithAjax
。
在另一個腳本標簽中,當選擇框更改時,我用以下代碼調用這些函數:
<script>
import fetchJson from 'some.module'
function displayJsonWithAjax() {
...
}
function displayOtherJsonWithAjax() {
...
}
</script>
<script>
$(document).ready(function () {
$('#selectBox').change(function () {
displayJsonWithAjax();
displayOtherJsonWithAjax();
}).change();
});
</script>
使用瀏覽器進行調試時,出現以下錯誤:
ReferenceError: displayJsonWithAjax is not defined
如果我嘗試將所有功能放在同一個腳本標簽中,那么瀏覽器加載頁面時將不會自動執行任何代碼...如何完成調用這兩個功能的工作?
import fetchJson from 'some.module'
這真的有用嗎? 檢查您的控制台。
如果腳本行失敗,則該行之后的所有內容都不會執行,因此腳本函數將不會被聲明,並且將無法在其他地方使用(這將解釋為什么“如果我嘗試將所有功能放在同一位置,腳本標記,當瀏覽器加載頁面時,不會自動執行任何代碼”:腳本在第1行失敗,並且不執行其他任何操作)。
test();
<script>function test(){ alert('hello'); }</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.