一个关于在window.onload里面定义函数,然后在html里面调用函数时出现错误。具体见下面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<script >
window.onload = function () {
//绑定两个输入框var Ow_before = document.getElementById('w_before');
var Ow_after = document.getElementById('w_after');
//定义调用函数bianhuan,然后在HTML中用button的点击事件onclik调用。
function bianhuan(the_option) {
switch (the_option) {
case 'covert_upper':
Ow_after.value = (Ow_before.value).toUpperCase();
console.log("大写转换成功!")
break;
case 'covert_lower':
Ow_after.value = (Ow_before.value).toLowerCase();
default:
console.log("转换失败!");
}
}
}
</script>
<body>
请输入英文字母:<input id="w_before" type="text"></br>
<input type="button" id="cover_upper" value="转大写" onclick="bianhuan('covert_upper

本文探讨了在JavaScript中使用window.onload时遇到的函数未定义错误。问题源于window.onload执行时,HTML尚未完全解析,导致函数无法找到。解决方案是将函数移出window.onload,放入全局作用域,并确保JS代码位于HTML body的底部,以遵循正确的加载执行顺序。
最低0.47元/天 解锁文章
263

被折叠的 条评论
为什么被折叠?



