maxLength不是函数

时间:2018-10-30 18:26:29

标签: javascript html

我的html页面有问题

此处是页面的简化版本

<html>
    <head>
        <script type="text/javascript">

            function maxLength()
            {
                console.log('maxLength');
            }

        </script>
    </head>
    <body>
        <input type"text" onblur="maxLength()">
    </body>
</html>

在onblur事件上,我收到此错误

未捕获的TypeError:maxLength不是函数

但是如果我从控制台调用该函数,它将正常工作并显示'maxLength'

为什么?

2 个答案:

答案 0 :(得分:4)

其中的代码在已定义input的{​​{1}}的{​​{3}}中运行。

更好的开发人员工具将使其更加清晰:

  

maxLength不是maxLength上的函数

将函数重命名为其他名称,或使用其他方式附加事件监听器。

答案 1 :(得分:0)

如果将函数重命名为其他名称,则应该可以正常使用。 Alex已经适当地给出了解释。示例:

<html>
    <head>
        <script type="text/javascript">

            function maximumLength()
            {
                console.log('maxLength');
            }

        </script>
    </head>
    <body>
        <input type"text" onblur="maximumLength()">
    </body>
</html>