代码之家  ›  专栏  ›  技术社区  ›  KeyC0de

包括bootstrap 4和jquery只能通过cdn工作,而不包括本地.js文件。为什么?

  •  0
  • KeyC0de  · 技术社区  · 7 年前

    我的代码如下。我只是按照下面的说明从网站复制和粘贴模式,以证明它仅在通过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。为什么会发生这种情况?我该如何解决?我错过了什么。

    我试过包括原始路径(没有 url_for() )结果相同。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Edgar Henriquez    7 年前

    href <script>

    < <-- This doesn't work src