我正在尝试运行我的 JavaScript,以便在单击 python flask 上的按钮时产生警报。我遇到的问题是 JS 文件第一行有一个错误:Uncaught ReferenceError: $ is not defined
。甚至交换第一个 $
用 jQuery 还是不行。
我的结构是:
app: - main.py
- static - js - mainJs.js
- lib - jquery-2.2.4.min.js
- jquery-ui.js
- css - stylesheet.css
- templates - index.html
我确定我正确导入了所有内容,因为我的 html 文件是:
<!DOCTYPE html>
<html>
<head>
<title>First doc</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/stylesheet.css') }}">
<script type="text/javascript" src="{{ url_for('static', filename = 'js/mainJs.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename = 'js/lib/jquery-2.2.4.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename = 'js/lib/jquery-ui.js') }}"></script>
</head>
<body>
{{err}}
<form id="commentForm" action="/addComment" method="POST">
<table>
<tr>
<td>
<textarea id="username" rows="1" cols="50" name="username"></textarea>
</td>
</tr>
<tr>
<td>
<textarea rows="4" cols="50" id="textArea" name="thetext"></textarea>
</td>
</tr>
</table>
<input id="rand" type ="submit" value="Submit" onclick = "substitute()" />
</form>
{% for item in Message %}
<table>
<tr>
<td>
{{item}}
</td>
</tr>
</table>
{% endfor %}
<div id="testDiv">
</div>
</body>
</html>
而我的 JavaScript 文件很简单:
$(document).ready(function(){
$("#rand").click(function(){
window.alert("its working!");
});
});
最佳答案
加载 .js
文件的顺序很重要,因为您在加载 jquery
之前在文件中引用 $
。
把你的文件导入,放在jquery之后解决问题。
关于javascript - Uncaught ReferenceError : $ is not defined python flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48853088/