简体   繁体   English

即使我的代码看起来很好,我仍然会收到“未捕获的TypeError:字符串不是函数”错误。任何人都可以看看并解释一下吗?

[英]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?

<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 accept function should be called. 每次用户单击按钮时,都应调用accept函数。 There's nothing wrong with the function itself, I'm sure about that. 功能本身没有任何问题,我确信这一点。 The function show that gets called when this starts just fills the selection with options, and create just sends it to the server. 该函数显示在启动时被调用只是用选项填充选择,而create只是将它发送到服务器。

You should put a semi-colon after the function call in your events: 你应该在事件中调用函数后放置一个分号:

 onload = "show();"

 onclick = "accept();"

Edit: I also see in these similar SO questions that there can be problems with function names that cause this exact error message. 编辑:我也在这些 类似的 SO问题中看到,可能存在导致此确切错误消息的函数名称问题 Just for fun, you might try renaming your functions... 只是为了好玩,您可以尝试重命名您的功能......

I have met the same question. 我遇到了同样的问题。 Then I figured it out that "download" is one of the reserved words of Chrome. 然后我发现“下载”是Chrome的保留字之一。 So I have to change the name of my javascript method. 所以我必须更改我的javascript方法的名称。

I think I know the root of the problem - I use AJAX to dynamically populate my page with HTML that is stored in variables. 我想我知道问题的根源 - 我使用AJAX用存储在变量中的HTML动态填充我的页面。 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: 例如:

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

Hope this is helpful to someone. 希望这对某人有帮助。

I had this same problem when using variables with popular names. 当使用具有流行名称的变量时,我遇到了同样的问题。

The variable z, for example, is used by jQuery Mobile. 例如,变量z由jQuery Mobile使用。 If you are using the same variable name and using a jQuery mobile widget, your use of the variable will be overwritten. 如果您使用相同的变量名称并使用jQuery移动小部件,则将覆盖您对变量的使用。

Better to avoid globals, of course. 当然,最好避免使用全局变量。 Better still to choose variable names not likely to lead to conflicts. 更好的是选择不太可能导致冲突的变量名称。 Var zZz for example. 例如, Var zZz

暂无
暂无

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

相关问题 我收到“未捕获的TypeError:无法读取属性&#39;removeChild&#39;为null”的错误,即使我的无序列表也能正常工作 - I receive a 'Uncaught TypeError: Cannot read property 'removeChild' of null' error even though my unordered list works fine 即使我的代码中有catch部分,我仍收到未处理的Promise Rejection错误 - I am getting Unhandled Promise Rejection error even though I have the catch section in my code 我的代码中不断出现“未捕获范围”错误,我不知道为什么 - I keep getting an 'uncaught range' error in my code and I can't figure out why Vue 无法识别我的组件,即使它对我来说似乎很好。 错误 - 未知的自定义元素:<componentName> - Vue can't recognize my component even though it seems fine to me. Error - Unknown custom element: <componentName> 任何人都可以向我解释为什么我会收到这个错误吗? “未捕获的类型错误 - Can Anybody please explain to me why I am getting this Error? "Uncaught TypeError 获取错误未捕获TypeError:string不是函数 - Getting error Uncaught TypeError: string is not a function 我收到错误Uncaught TypeError:无法读取未定义的属性&#39;drawBox&#39;,有人可以发现问题吗? - I'm getting the error Uncaught TypeError: Cannot read property 'drawBox' of undefined, Can anyone spot the issue? 得到一个未捕获的类型错误,它阻止了一个我不确定如何修复的分数警报,即使我的游戏正常运行 - Getting an Uncaught TypeError that is preventing a score alert that I'm unsure how to fix even though my game plays as normal 我不断收到一条错误消息“未捕获的类型错误:无法读取未定义的属性‘点击’”有人知道怎么了吗? - I keep getting an error saying “Uncaught TypeError: Cannot read property 'click' of undefined” does anyone know what's wrong? 我不断收到错误“ main.js:19 Uncaught TypeError:firebase.database不是函数” - I keep getting the error “main.js:19 Uncaught TypeError: firebase.database is not a function”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM