【发布时间】:2018-07-28 21:59:17
【问题描述】:
我正在尝试运行我的 JavaScript,以便在单击 python 烧瓶上的按钮时产生警报。我遇到的问题是 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!");
});
});
【问题讨论】:
-
在你的 javascript 标签上使用
defer属性并在关闭正文之前添加它
标签: javascript python html flask