【发布时间】:2015-04-13 02:02:35
【问题描述】:
有了这个 index.html:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script data-main="main" src="require.js"></script>
</head>
<body>
<p>This is the body</p>
<canvas id="canvas1"></canvas>
</body>
</html>
还有这个 main.js
console.log("main.js ran");
function render() {
console.log("render ran");
}
window.onload = render;
我希望控制台输出显示:
main.js ran
render ran
“main.js 运行”按预期显示,但“渲染运行”没有被记录。渲染函数永远不会被调用。为什么不呢?
【问题讨论】:
-
嗯,奇怪。在我看来一切都很好。
-
你如何加载
main.js?我怀疑你是在加载窗口后加载它,所以window.onload函数运行为时已晚。 -
@Bermar,很好的发现,但是如果 Vocalionecho 链接了错误的 javascript 文件,为什么 main.js 运行会出现在控制台中。从代码 sn-p 来看,
console.log()都在同一个文件中。 -
尝试添加
-
@NewToJS 这就是为什么我认为问题是他在
window.onload运行后加载文件。
标签: javascript html requirejs