【问题标题】:Uncaught ReferenceError: $ is not defined python flask未捕获的ReferenceError:$未定义python烧瓶
【发布时间】: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


【解决方案1】:

加载.js 文件的顺序很重要,因为您在加载jquery 之前在文件中引用$

把你的文件导入,放在jquery之后解决问题。

【讨论】:

  • 我明白了,这解决了问题。谢谢楼主!
猜你喜欢
  • 1970-01-01
  • 2023-01-23
  • 2016-11-03
  • 2011-01-05
  • 2016-01-02
  • 2013-10-06
  • 2016-12-17
相关资源
最近更新 更多