简体   繁体   English

在Bottle中导入Javascript文件(框架jQuery)

[英]Import Javascript file in Bottle (framework jQuery)

I have a problem when I try to run my HTML file with javascript in Bottle. 当我尝试在Bottle中使用javascript运行HTML文件时遇到问题。 Have a look at my code: 看一下我的代码:

bottle server: 瓶服务器:

from bottle import static_file
...
@route('/static/<filename>')
def static():
return static_file(filename, root='.')
...

HTML file: HTML档案:

...
<input type="text" id="time" class="datepicker" required>
...
<script type="text/javascript" src="static/jquery-ui/jquery-2.1.4.js"></script>
<script type="text/javascript" src="static/jquery-ui/jquery-ui.js"></script>
<link rel="stylesheet" href="static/jquery-ui/jquery-ui.css">
<script type="text/javascript" src="static/jquery-ui/my-script.js"></script>

Javascript file: Javascript文件:

$(function() {
$('.datepicker').datepicker();
});

HTML file runs correctly when I don't use Bottle (so without using style/ ) but not from Bottle. 当我不使用Bottle时,HTML文件可以正确运行(因此,如果不使用style/ ),而不能从Bottle中运行。 I use a method static() to return the url of my javascript file, this method works for css file, but no result for javascript... 我使用static()方法返回我的javascript文件的url,此方法适用于css文件,但不适用于javascript ...

Instead of hard-coding the paths, I would suggest to use Bottle's get_url function.If you publish your application under a directory (fe domain.com/app) it won't work. 我建议不要使用Bottle的get_url函数来对路径进行硬编码。如果将应用程序发布在目录下(fe domain.com/app),它将无法正常工作。 So better use src="{{ get_url('static', filename='jquery-ui/jquery-2.1.4.js') }}" 因此最好使用src="{{ get_url('static', filename='jquery-ui/jquery-2.1.4.js') }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM