jquery - 使用 Flask-Bootstrap 时在 jquery-ui 之前加载 jquery

标签 jquery python flask

我需要加载jquery-ui来实现jquery的自动完成功能,flask-bootstrap没有。我用这一行将它添加到模板中

<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js" ></script>

但是,我让 Flask-Bootstrap 加载 jquery.js,它总是在模板加载 jquery-ui 之后加载它,因此 jquery-ui 失败,因为它需要先加载 jquery。

GET http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css [HTTP/1.1 200 OK 123ms]
GET http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css [HTTP/1.1 200 OK 181ms]
GET http://code.jquery.com/ui/1.11.2/jquery-ui.js [HTTP/1.1 200 OK 324ms]
GET http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.js [HTTP/1.1 200 OK 289ms]
GET http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js [HTTP/1.1 200 OK 111ms]
GET http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment-with-langs.min.js [HTTP/1.1 200 OK 175ms]
ReferenceError: jQuery is not defined jquery-ui.js:14:2
ReferenceError: $ is not defined

为了暂时解决这个问题,我修改了flask-bootstrap的__init.py__使其不加载jquery,在模板中手动加载。此页面有效,但其他所有页面都停止工作,因为不再加载 jquery。

有没有办法确保 flask-bootstrap 先加载 jquery,然后让模板加载 jquery-ui 或 flask-bootstrap 加载 jquery-ui 本身?

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block head %}
    {{ super() }}
    <link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="Stylesheet"></link>
    <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js" ></script>
{% endblock %}

{% block title %}Project-Z{% endblock %}

{% block page_content %}
<div class="page-header">
    <h1> Members list </h1>
    <h2>Find a member</h2>
    <br>
    <div class="col-md-4">
        {{ wtf.quick_form(form) }}
    </div>
</div>

<script type="text/javascript">
    $(function() {
        $( "#member_name" ).autocomplete({
            source: '{{url_for("main.autocomplete")}}',
            minLength: 2,
        });
    });
</script>
{% endblock %}

最佳答案

我没有将脚本放在头部,而是将它放在脚本部分,以便它按顺序加载。这样它就可以在 Flask-bootstrap jquery.js 加载后正确加载。 http://pythonhosted.org/Flask-Bootstrap/faq.html#how-can-i-add-custom-javascript-to-the-template

{% block scripts %}
  {{super()}}
   <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js" ></script>
{% endblock %}

关于jquery - 使用 Flask-Bootstrap 时在 jquery-ui 之前加载 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29877610/

相关文章:

jquery - 在滚动和停止方法排队时优化 JQuery

python - 如何使用 pyparsing Group 和 SkipTo 进行文件解析?

python - 构建游戏,需要有关库存的建议

python - 在服务器上运行 selenium 浏览器 (Flask/Python/Heroku)

javascript - 使用相同的 jQuery 函数但在 HTML 中导致冲突

javascript - 用 Javascript 替换页面上的电话号码

javascript - 我的菜单溢出空间

Python JSON 到 CSV - 编码错误,UnicodeDecodeError : 'charmap' codec can't decode byte

python - 自定义flask-security的@auth_token_required未经授权的响应

python - Flask:将用户重定向到 html 页面的特定部分