未捕获的TypeError:lang不是函数

编程入门 行业动态 更新时间:2024-10-27 19:27:56
本文介绍了未捕获的TypeError:lang不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的HTML中,我在脚本标记中定义 lang 函数并添加Test Fire!点击时必须调用 lang 的按钮:

In my HTML I define the lang function in the script tag and add the "Test Fire!" button which has to call lang on click:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Testing Functions</title> <script type="text/javascript"> function lang() { alert("Hello, World! It's JavaScript this time"); } </script> </head> <body> <form action=""> <input type="button" value="Test Fire!" onclick="lang();"> </form> </body> </html>

但是,如果我单击按钮我会收到此错误:

However, if I click the button I get this error:

未捕获TypeError: lang 不是函数

但如果我从 lang 对于其他任何代码都可以正常工作。

But if I change the function name from lang to anything else this code works fine.

推荐答案

考虑以下代码:

<input type="button" value="Test Fire!" onclick="debugger;" />

当你打开调试器时,你会看到似乎有一个隐含的带范围的 onclick ,使所有的< ;输入> 的属性无需参考按钮即可访问。

When you open your debugger, you’ll see that there seems to be an implicit with scope wrapped around the onclick, making all the <input>’s properties accessible without needing to refer to the button.

这意味着所有全局属性( lang 就是其中之一),其他几个特定于按钮的覆盖被覆盖。例如。 value , type 也不起作用。

This means that all global attributes (lang is one of them) and several others specific to buttons are overridden. E.g. value, type also wouldn’t work.

您最好的选择是使用添加事件侦听器的标准方法: addEventListener 。

Your best bet is to use the standard way of adding event listeners: addEventListener.

<input type="button" value="Test Fire!" /> <script> function lang() { alert("Hello, World! It's JavaScript this time"); } document.querySelector("input").addEventListener("click", lang); </script>

更多推荐

未捕获的TypeError:lang不是函数

本文发布于:2023-11-23 03:46:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   TypeError   lang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!