简体   繁体   English

嗯,无法调用javascript函数

[英]Hm, cannot call javascript function

Why this not works: 为什么这样不起作用:

I have main.html: 我有main.html:

<script type="text/javascript" src="some_url"></script>
.
.
<a href="javascript:void(0)" onClick="Foo()">Foo</a></li>

Function Foo is located in external javascript file. Foo函数位于外部javascript文件中。 With this code I cannot call function but if I put that external js into main.html it works. 使用此代码,我无法调用函数,但是如果我将外部js放入main.html中,它将起作用。 Where is problem? 问题在哪里? I guess it's something stupid :) 我想这有点愚蠢:)

NEW UPDATE 新更新

Well I'm using Django framework and external files are loaded using django static directive: 我正在使用Django框架,并且使用django static指令加载了外部文件:

<script type="text/javascript" src="{% static 'st_forms/main.js' %}"></script>

function is called like this: 函数的调用方式如下:

<a href="javascript:void(0)" onClick="goForm('RHJ6YXZh')">Form</a></li>

and external main.js file (the whole file): 和外部main.js文件(整个文件):

function goForm(formCode)
{
    alert("entered");
}

I hope this is enough... 我希望这足够了...

Path to main.js is rendered successfully to "/static/st_forms/main.js" and it is accessible by browser. main.js的路径已成功呈现到“ /static/st_forms/main.js”,并且可通过浏览器访问。

You are probably adding your function Foo after the HTML is loaded, on window load , or document ready ... 您可能在HTML加载, window loaddocument ready后添加了Foo函数。

Look at this example: 看这个例子:

Fiddle 小提琴

<script>
    function foo() {
        alert("Foo called!");
    }

    window.onload = function(){
        function bar() {
            alert("Bar called!");
        }
    }
</script>

<a href="javascript:void(0)" onClick="foo()">Foo</a>
<a href="javascript:void(0)" onClick="bar()">Bar</a>

On click Foo is called and Bar is not. 单击时,将调用Foo ,而不会调用Bar

try: onclick="Foo()">Foo</a></li> instead of original: onClick="Foo()" 试试: onclick="Foo()">Foo</a></li>而不是原始的: onClick="Foo()"

lowercase 'onclick'. 小写的“ onclick”。

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

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