Javascript 我不断收到“未捕获的类型错误:字符串不是函数”错误,即使我的代码似乎没问题。任何人都可以看看并解释一下吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8810787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:37:23  来源:igfitidea点击:

I keep getting a "Uncaught TypeError: string is not a function" error, even though my code seems to be fine. Can anyone have a look and explain?

javascripthtml

提问by corazza

<html>
    <head>
    <script language = javascript>
    show = function()
    {
         document.getElementById("name").innerHTML = window.opener.game.pl.guild.n;
         var app = document.getElementById("app");
         var apps = window.opener.game.pl.guild.app;

         for (a in apps)
         {
              var appAdd = document.createElement("option");
              appAdd.text = apps[a];
              appAdd.value = apps[a];

              app.add(appAdd);
         }
    }

    accept = function()
    {
        console.log("Accepting");
        var app = document.getElementById("app");

        window.opener.input.options = "acceptApp";
        window.opener.input.an = app.options[app.selectedIndex].value;
    }
    </script>
    </head>

    <body onload = "show()">
        <h1 id = "name" align = "center"> ### </h1>
    <div style="text-align:center">
            <p>You own this guild.</p>
        </div>
    </br>

        <p>Applications:</p>
        <select id = "app"></select>

        <input type = "button" onclick = "accept()" value = "Accept application" />
     </body>
</html>

Every time a user clicks the button, the acceptfunction should be called. There's nothing wrong with the function itself, I'm sure about that. The function showthat gets called when this starts just fills the selection with options, and createjust sends it to the server.

每次用户单击按钮时,都应调用接受函数。函数本身没有问题,我确信这一点。开始时调用的函数show只是用选项填充选择,而create只是将它发送到服务器。

回答by DOK

You should put a semi-colon after the function call in your events:

您应该在事件中的函数调用后放置一个分号:

 onload = "show();"

 onclick = "accept();"

Edit: I also see in thesesimilarSO questions that there can be problems with function namesthat cause this exact error message. Just for fun, you might try renaming your functions...

编辑:我还在这些类似的SO 问题中看到,函数名称可能存在问题,导致此确切错误消息。只是为了好玩,你可以尝试重命名你的函数......

回答by sky

I have met the same question. Then I figured it out that "download" is one of the reserved words of Chrome. So I have to change the name of my javascript method.

我遇到了同样的问题。然后我发现“下载”是Chrome的保留字之一。所以我必须更改我的 javascript 方法的名称。

回答by Wouter Tichelaar

I think I know the root of the problem - I use AJAX to dynamically populate my page with HTML that is stored in variables. I had the exact same problem when I named my function the same as my variable, it would seem as if the browser was trying to run the variable as a function. For instance:

我想我知道问题的根源 - 我使用 AJAX 用存储在变量中的 HTML 动态填充我的页面。当我将函数命名为与变量相同的名称时,我遇到了完全相同的问题,看起来好像浏览器试图将变量作为函数运行。例如:

var callthis = 'fjdashfkjdh';
function callthis() {
    alert(callthis);
}

HTML:

HTML:

<button onclick="callthis();" type="button">Click Me</button>

Clicking the button will cause the Javascript parser to run through the Javascript code, and when it hits the first line it runs the variable instead of seeking a function. This is perhaps a bug in Javascript itself because it should look for the first function with the same name, not the first name which is equal to the function to be called e.g. callthis();.

单击该按钮将导致 Javascript 解析器运行 Javascript 代码,当它到达第一行时,它运行变量而不是寻找函数。这可能是 Javascript 本身的一个错误,因为它应该查找第一个具有相同名称的函数,而不是与要调用的函数相同的第一个名称,例如callthis();.

Hope this is helpful to someone.

希望这对某人有帮助。

回答by Learning stats by example

I had this same problem when using variables with popular names.

在使用具有流行名称的变量时,我遇到了同样的问题。

The variable z, for example, is used by jQuery Mobile. If you are using the same variable name and using a jQuery mobile widget, your use of the variable will be overwritten.

例如,变量 z 由 jQuery Mobile 使用。如果您使用相同的变量名并使用 jQuery 移动小部件,您对变量的使用将被覆盖。

Better to avoid globals, of course. Better still to choose variable names not likely to lead to conflicts. Var zZzfor example.

当然,最好避免全局变量。最好选择不太可能导致冲突的变量名称。Var zZz例如。