首页 > 解决方案 > 如何修复 HTML onclick 事件中的“未定义函数”?

问题描述

我正在使用 Visual Studio Code 在 HTML 中编写源代码想法是创建与功能链接的按钮,这些按钮的上下文是关于我的信息,例如:姓名、爱好等。这是我的完整源代码

<!DOCTYPE html>
<html>
<head lang="ja">
<meta charset="utf-8">
<title>INTRODUCE</title>
</head>
<body>
  <h1><strong>自己紹介</strong></h1> 
  <button onclick="myname()">Name</button><br><br>
  <button onclick="age()">Age</button><br><br> 
  <button onclick="homeTown()">Hometown</button><br><br>
  <button onclick="hobby()">Hobby</button><br><br>
  <button onclick="other()">Other</button><br><br>
  <button onclick="all()">All</button><br><br>
  <b><結果はコンソールで確認&gt;</b>

<script>

  function myname()
  {
  let name = "ABFFD";
  let str = "ベトナムの首都であるハノイです&quot;;
  console.log("私は"+ name+"です.");
   }
  function age(){
  let age = 23;
  console.log(age);
   }
  function homeTown()
   {
  console.log(str);
  console.log("ハノイでは名所と有名な食べ物が多くありますので,以下のリンクをご覧くだ 
  さい!&quot;);
  <a href ="https://www.whyweseek.com/must-try-vietnamese-foods-hanoi/" 
  target="_blank">ハノイでの有名な食べ物&lt;/a>;
  <a href ="http://www.vietnam-guide.com/hanoi/top10/top10-hanoi- 
  attractions.htm" target="_blank">ハノイの名所</a>;
  }
  function hobby()
  {
console.log("私の趣味は&quot;);
<a href="../ウエブプログラミングI/html/myhobby.html">こちら&lt;/a>;
  }
function other()
{
let str1='身長', str2='体重';
let a = 180, b= 64;
console.log(str1+"は&quot;+toString(a)+"cm");
console.log(str2+"は&quot;+toString(b)+"kg");
}

</script>
</body>
</html>

但是当我在 Web 控制台中编译它时,它有这样的“未定义功能” 在此处输入图像描述

我之前编写的其他源代码运行良好,但我遇到了麻烦。任何人都可以提出解决方案吗?谢谢

标签: javascripthtml

解决方案


感谢您的所有评论。我想我已经知道了这个问题!这不是关于 onclick 按钮,而是关于我的功能定义问题导致我的网站无法加载我认为的数据。我删除 了函数hometown()hobby()中的那些链接href="...."然后它们运行良好。然后我想我必须使函数other()all()中的这些变量成为全局变量才能使其正常工作!


推荐阅读