包括Bootstrap 4和JQuery仅通过CDN起作用,而不包括本地.js文件。为什么?

时间:2018-07-13 17:01:20

标签: python flask bootstrap-4 bootstrap-modal flask-wtforms

我的代码如下。我只是按照以下指定的方式从网站上复制并粘贴了模式,以演示它仅在通过CDN添加.js文件时有效。

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Bootstrap required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- load my css styles -->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='mine.css') }}">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='bootstrap_4.1.1.min.css') }}">
</head>
<body>
    <header class="site-header">
        ....
    </header>

    <main role="main" class="container">
        .....
    </main>

    <!-- Copy paste modal from official website @ https://getbootstrap.com/docs/4.0/components/modal/#live-demo -->
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
        Launch demo modal
    </button>

    <button type="button" class="swal">
        swal
    </button>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <div class="modal-body">

                    <button type="button" class="swal">
                        swal
                    </button>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>


    <!-- jQuery first, then Popper.js, then Bootstrap JS -->

    <!-- This works -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
    crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

    <!-- This doesn't work
    <script type="text/javascript" href="{{ url_for('static',filename='libs/jquery-3.2.1.min.js') }}"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
            crossorigin="anonymous"></script>
    <script type="text/javascript" href="{{ url_for('static',filename='libs/bootstrap_4.1.1.min.js') }}"></script>
    -->
</body>
</html>

为什么本地包含javascript文件不起作用?我已经确认,这是static文件夹内的正确路径。我宁愿包含本地文件,也不愿依赖CDN。为什么会发生这种情况,我该如何解决?我想念什么。

ps。我尝试过包含具有相同结果的原始路径(无url_for())。

1 个答案:

答案 0 :(得分:1)

之所以不起作用,是因为您试图在href标签上使用<script>属性。

如您所见,在<script>行下方的<-- This doesn't work标记中,有3个标记中有2个使用href而不是src属性,请对其进行更改,只要路径是正确的路径,应该可以正常工作。