【发布时间】:2018-10-18 11:12:18
【问题描述】:
我收到错误:“Uncaught ReferenceError: $ is not defined”。
下面是component/templates/component/update.html模板,定义为:
{% extends 'base.html' %}
{% block content %}
{% load static %}
<script src="{% static 'component/js/component.js' %}"></script>
<h2>Create new component</h2>
{% include 'snippets/form-snippet.html' with form=form %}
{% endblock %}
我将脚本保存在 component/static/component/js/component.js 中。找到该文件是因为在 console.log 中我可以读到:“给我一些日志”。但是我收到错误:“未捕获的 ReferenceError:$ 未定义”。显然没有找到 jQuery,但如何解决这个问题? component.js 文件定义如下:
console.log("GIVE ME SOME LOG")
$(document).ready(function(){
hideShow()
})
$('#id_component_type').click(function(){
hideShow()
});
function hideShow(){
if(document.getElementById('id_component_type').options[document.getElementById('id_component_type').selectedIndex].value == "k_v")
{
$('#id_length').parents('p:first').hide();
$('#id_k_v').parents('p:first').show();
}else
{
$('#id_length').parents('p:first').show();
$('#id_k_v').parents('p:first').hide();
}
}
base.html文件定义为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block head_title %}Solarus Calculation Tool{% endblock head_title %}</title>
{% include 'snippets/css.html' %}
</head>
<body>
{% include 'snippets/nav.html' %}
<div class='container'>
{% block content %}
{% endblock content %}
</div>
{% include 'base/js.html' %}
<script>
$(document).ready(function(){
{% block jquery %}{% endblock %}
})
</script>
</body>
</html>
js.html定义为:
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js'></script>
【问题讨论】:
-
我要问的是显而易见的。项目中是否包含 jQuery?比如像这样:stackoverflow.com/questions/11525994/…
-
你的 base.html 怎么样?让我们看看你的 js 调用的结构
-
我添加了base.html和js.html