javascript - 如何修复 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><結果はコンソールで確認></b>
<script>
function myname()
{
let name = "ABFFD";
let str = "ベトナムの首都であるハノイです";
console.log("私は"+ name+"です.");
}
function age(){
let age = 23;
console.log(age);
}
function homeTown()
{
console.log(str);
console.log("ハノイでは名所と有名な食べ物が多くありますので,以下のリンクをご覧くだ
さい!");
<a href ="https://www.whyweseek.com/must-try-vietnamese-foods-hanoi/"
target="_blank">ハノイでの有名な食べ物</a>;
<a href ="http://www.vietnam-guide.com/hanoi/top10/top10-hanoi-
attractions.htm" target="_blank">ハノイの名所</a>;
}
function hobby()
{
console.log("私の趣味は");
<a href="../ウエブプログラミングI/html/myhobby.html">こちら</a>;
}
function other()
{
let str1='身長', str2='体重';
let a = 180, b= 64;
console.log(str1+"は"+toString(a)+"cm");
console.log(str2+"は"+toString(b)+"kg");
}
</script>
</body>
</html>
但是当我在 Web 控制台中编译它时,它有这样的“未定义功能”
我之前编写的其他源代码运行良好,但我遇到了麻烦。任何人都可以提出解决方案吗?谢谢
解决方案
感谢您的所有评论。我想我已经知道了这个问题!这不是关于 onclick 按钮,而是关于我的功能定义问题导致我的网站无法加载我认为的数据。我删除 了函数hometown()和hobby()中的那些链接href="...."然后它们运行良好。然后我想我必须使函数other()和all()中的这些变量成为全局变量才能使其正常工作!
推荐阅读
- angular - 带有 Angular Material 声明的 Angular 编译警告
- java - AsyncEventHandler - 实时系统 Java
- python-3.x - 如果根是框架,我们可以为入口小部件使用高度属性吗
- typescript - 如何检查对象内的数组是否包含特定值?
- python - TypeError: unorderable types: float() < str() while using fit_transform of LabelBinarizer
- excel - 在excel中根据尺寸值绘制形状
- linux - 如何添加详细选项来控制脚本输出?
- javascript - 如何在 Three.js 中创建散景(或以任何方式以编程方式创建散景)
- javascript - How do I collapse all extended blocks?
- python - 试图用另一个 int 减去一个 int 但我得到一个错误