[英]Flask JavaScript/HTML Scripts Not Working
我有一個正在創建的flask Web應用程序(也使用Angular CLI),並且有一個按鈕。 我正在嘗試使用JavaScript對按鈕單擊進行操作,但是由於任何原因,我都無法使JavaScript正常工作。
但是,當我將Java腳本復制到完全不同的簡單flask項目(我是從在線下載的)時,是否可以正常工作?
有誰知道我是否必須在flask項目中做點什么才能使javascript工作?
我在某處閱讀了與運行flask應用程序的方式有關的信息? (0.0.0.0 vs localhost)但我不確定
這是我要使用的代碼:
<!DOCTYPE html>
<html>
<body>
<h1>The onclick Event</h1>
<p>The onclick event is used to trigger a function when an element is clicked on.</p>
<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
您將函數結果作為參數而不是函數傳遞。 采用
onclick="myFunction"
但是,如果要以專業的方式進行操作,則應該這樣做:
<!DOCTYPE html>
<html>
<body>
<h1>The onclick Event</h1>
<p>The onclick event is used to trigger a function when an element is clicked on.</p>
<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>
<button>Click me</button>
<p id="demo"></p>
<script>
document.querySelector('button').addEventListener('click', myFunction)
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
使用innerHTML插入數據也不是最佳選擇。 在javascript中,您可以通過編程方式創建DOM elementr。
如果沒有顯示console.log,請嘗試此操作,這是有關如何編譯的問題
document.addEventListener('DOMContentLoaded',()=>{ console.log('loaded) let btn = document.querySelector('button') btn.addEventListener('click', myFunction) function myFunction(e) { console.log(e); document.getElementById("demo").innerHTML = "Hello World"; } })
從根目錄中的“靜態”文件夾嘗試。 從字面上稱它為靜態,然后將js文件放入其中。 然后在html中試試這個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.