<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button onclick="start()">111</button>
<script>
window.onload = function() {
function start() {
alert('111');
}
}
</script>
</body>
</html>
在html代码中使用事件绑定函数,但浏览器会报错
test.html:8 Uncaught ReferenceError: start is not defined
at HTMLButtonElement.onclick (test.html:8)
但把start函数放在onload之外,就可以正常执行。想问一下整个加载过程是怎样执行的?
和加载没关系 你在标签里写的事件的方法得在window对象下`
window.onload = function() {
`
写成这样