我尝试了以下代码,但firefox4没有显示任何内容..
<script type="text/javascript">
var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d');
context.strokeStyle = '#990000';
context.strokeRect(20,30,100,50);
alert(context);
</script>
<canvas id="canvas1" width="200px" height="200px">Your browser does not support canvas </canvas>
此代码位于正文标记内。
答案 0 :(得分:3)
您必须了解浏览器执行文件的顺序。 以下是浏览器读取此代码的方式:
<script type="text/javascript">
var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d');
context.strokeStyle = '#990000';
context.strokeRect(20,30,100,50);
alert(context);
</script>
<canvas id="canvas1" width="200px" height="200px">Your browser does not support canvas </canvas>
canvas1
nothing.getContext('2d');
alert(nothing)
因此,为了让它可见,你只需编辑你的代码: -
<!DOCTYPE HTML>
<html>
<head>
<title>Sample by RHNVRM(aka rohan verma)</title>
</head>
<body>
<!--Canvas-->
<canvas id="canvas1" width="200px" height="200px">Your browser does not support canvas </canvas>
<!--Begin Script-->
<script>
var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d');
context.strokeStyle = '#990000';
context.strokeRect(20,30,100,50);
alert(context);
</script>
</body>
</html>
注意: 使用Javascript时,无需在HTML5中提及。
答案 1 :(得分:0)
根据此搜索http://www.google.co.uk/search?q=firefox4+canvas,firefox 4支持画布。
答案 2 :(得分:0)
这对我来说很好:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<canvas id="canvas1" width="200px" height="200px">Your browser does not support canvas </canvas>
<script type="text/javascript">
var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d');
context.strokeStyle = '#990000';
context.strokeRect(20,30,100,50);
alert(context);
</script>
<body>
</body>
</html>