Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从django中的另一个模板调用基模板的javascript函数_Javascript_Html_Django - Fatal编程技术网

从django中的另一个模板调用基模板的javascript函数

从django中的另一个模板调用基模板的javascript函数,javascript,html,django,Javascript,Html,Django,在我的base.html模板中,我编写了一个函数。我可以从另一个模板调用它吗 我试过了。它不起作用 base.html: <!-- ...code... --> <script> function registration(){ if(document.getElementById("registration").className==='hide'){ document.getE

在我的base.html模板中,我编写了一个函数。我可以从另一个模板调用它吗

我试过了。它不起作用

base.html:

<!-- ...code... -->
<script>
        function registration(){
            if(document.getElementById("registration").className==='hide'){
                document.getElementById("registration").className='show'
            }else{
                document.getElementById("registration").className='hide'
            }
        }
</script>

函数注册(){
if(document.getElementById(“registration”).className=='hide'){
document.getElementById(“注册”).className='show'
}否则{
document.getElementById(“注册”).className='hide'
}
}
另一个模板

{% extends 'base.html' %}
{% block body %}
<script>
    //if i re write the function here, it works
    registration()
</script>

{% endblock body %}
{%extends'base.html%}
{%block body%}
//如果我在这里重新编写函数,它会工作
注册()
{%endblock body%}

您可以在
base.html

<html>
<head>
<!---Here all the CSS links --->

{% block css %}
{% endblock css %}

<head>

<body>
{% block body %}
{% endblock body %}
</body>

{% block script %}
 //write your script here
{% endblock script %}
</html>
{% block body %}
{% endblock body %}

{%block body%}{%endblock%}
是在base.html中定义了
函数注册()
之前还是之后?我在标记ok之前写{%block body%}{%endblock%},所以这里的问题是
标记是按顺序计算的。因此,您试图在函数实际定义之前调用它。您可以创建另一个位于其他
标记下方的块,例如
{%block extra%}
,您可以在其中插入其他模板中的其他
标记。