扩展HTML python烧瓶中无法识别jQuery [英] jQuery not recognized in extended html python flask

查看:56
本文介绍了扩展HTML python烧瓶中无法识别jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这是怎么回事.看来jQuery并未从base.html文件扩展"到外部文件.

Not sure what is going on here. It seems that jQuery is not "extended" from a base.html file to an external one.

我有:

#base.html

<!doctype html>
<html class="no-js" lang="en">

<head>
...
... # css imports
</head>

<body>

   # some fixed stuff

   {% block body %}

      # some stuff specific to base.html

   {% endblock %}

   # js imports at the end of the body

   <script src="static/js/jquery-3.1.0.min.js"></script>

    ... # various other js
</body>
</html>

然后我还有另一个html文件:

Then I have another html file:

#test.html

{% extends "base.html" %}

{% block body %}

    # some stuff

   <script type="text/javascript">

        $(document).ready( function () {
             alert("foo")
         } );

    </script>

{% endblock %}

现在,我没有任何警报.但是,如果我使用普通的javascript,它将按预期工作.

Now, I don't get any alert. However, if I use plain javascript it works as expected.

我注意到,如果我再次在test.html中导入jQuery,则jQuery可以正常工作.但是,扩展的目的是什么?

I noticed that if I import again jQuery in test.html, jQuery works fine. But what's the point of extending then?

肯定有我所缺少的东西.顺便说一下,这似乎只发生在jQuery上,所有其他javascript库似乎都可以扩展.

There must be something that I'm missing. By the way, this seems to happen only with jQuery, all other javascript libraries seem to be extended fine.

推荐答案

这真的非常简单.当以下代码运行时,需要使用jQuery运行.

It's really very simple. When the following code runs, it needs to run using jQuery.

<script type="text/javascript">

    $(document).ready( function () {
         alert("foo")
     } );

</script>

但是,在这些命令之后会加载jQuery,而在此之前需要将其放置.

However, your jQuery is being loaded AFTER these commands, whereas, it needs to be placed BEFORE that.

这就像在将水倒入玻璃杯之前先从玻璃杯中喝水.您需要先将水倒入玻璃杯中,然后再饮用.我不知道这个例子是否容易实现.

It's like you're drinking water from a glass before you're putting water in it. You need to put the water in the glass first and then drink it. I don't know if this example makes it easy or not.

这篇关于扩展HTML python烧瓶中无法识别jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆