【问题标题】:(index):69 Uncaught TypeError: name is not a function at HTMLButtonElement.onclick ((index):69) [duplicate](index):69 Uncaught TypeError: name is not a function at HTMLButtonElement.onclick ((index):69) [重复]
【发布时间】:2017-09-02 13:53:53
【问题描述】:

我只是在 JavaScript 中创建一个程序,然后弹出此错误。我已经研究过了,但所有的案例似乎都与我的不同。任何帮助将不胜感激。

 function name() {
       var divi = document.getElementById('form_div');
       divi.style.display = 'none';
}
<div id='form_div'>

    <form id='div_form'>
        <input maxlength="15" style='position: center;' type="text"  placeholder="Nick" id="namebox"/>
    </form>
    
    <button onclick='name();'>Go!</button>
    <p>Type in your nick and press go!</p>
</div>

(这个脚本元素放在上面显示的html之前)

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您定义的函数名称出错,如果您更改它,代码可以正常工作。

    您还应该避免使用 JavaScript 内置对象、属性和方法的名称。 参考:JavaScript Reserved Words

    <div id='form_div'>
    <form id='div_form'>
    <input maxlength="15" style='position: center;' type="text"  
    placeholder="Nick" id="namebox"/>
    </form>
    <button onclick='functionName()'>Go!</button>
    <p>Type in your nick and press go!</p>
    </div>
    <script>
    function functionName() {
       var divi = document.getElementById('form_div');
       divi.style.display = 'none';
    }
    
    </script>

    【讨论】:

    • 感谢您的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多