Javascript没有显示在浏览器中 [英] Javascript not showing up in browser
问题描述
我在本地主机上制作我的网络应用程序,但浏览器页面一直显示为空白,我做错了什么?谢谢
< html>
< head>
< script>
window.onload = function(){
var canvas = document.getElementById(canvas),
ctx = canvas.getContext(2d),
W = window.innerWidth,
H = window.innerHeight;
setInterval(draw,33);
函数draw(){
ctx.globalcompositeOperation =source-over;
ctx.fillStyle =black;
ctx.fillRect(0,0,W,H);
}
}
< / script>
< / head>
< body>
< canvas id =canvas>< / canvas>
你在分配给 onload
的匿名函数中声明了一堆变量。它们不是全局变量。
然后尝试(在一个时间间隔内)将它们作为全局变量来访问。
您在开发人员工具中查看控制台,您应该会看到一堆 ReferenceErrors 。
$ b 移动 setInterval
和绘制
函数声明放入您分配给 onload $ c $的匿名函数C>。
window.onload = function(){var canvas = document.getElementById(canvas),ctx = canvas.getContext(2d ),W = window.innerWidth,H = window.innerHeight; setInterval(draw,33);函数draw(){ctx.globalcompositeOperation =source-over; ctx.fillStyle =black; ctx.fillRect(0,0,W,H); }}
< canvas id =canvas> < / canvas>
I am making my web app on localhost, but the browser page keeps showing up as blank, what am I doing wrong? Thanks
<html>
<head>
<script>
window.onload = function() {
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
W = window.innerWidth,
H = window.innerHeight;
setInterval(draw, 33);
function draw() {
ctx.globalcompositeOperation = "source-over";
ctx.fillStyle = "black";
ctx.fillRect(0,0,W,H);
}
}
</script>
</head>
<body>
<canvas id="canvas"></canvas>
You declare a bunch of variables inside the anonymous function you assign to onload
. They are not globals.
You then try to (on an interval) access them as globals.
If you look at the Console in your developer tools you should expect to see a bunch of ReferenceErrors.
Move setInterval
and the draw
function declaration inside the anonymous function you assign to onload
.
window.onload = function () {
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
W = window.innerWidth,
H = window.innerHeight;
setInterval(draw, 33);
function draw() {
ctx.globalcompositeOperation = "source-over";
ctx.fillStyle = "black";
ctx.fillRect(0, 0, W, H);
}
}
<canvas id="canvas"></canvas>
这篇关于Javascript没有显示在浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!