您之前没有包含jQuery核心脚本,这就是它失败的原因.另外,我建议使用jQuery函数来添加属性,而不是使用字符串.
<!doctype html> <html lang="en"> <head> <meta charset='utf-8'> <title>Test Game</title> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> </head> <body> <script type="text/javascript"> var CANVAS_WIDTH = 480; var CANVAS_HEIGHT = 320; var canvasElement = $("<canvas>") .attr('width', CANVAS_WIDTH) .attr('height', CANVAS_HEIGHT); var canvas = canvasElement.get(0).getContext("2d"); canvasElement.appendTo('body'); var FPS = 30; setInterval(function () { update(); draw(); }, 1000/FPS); function update () { } function draw () { canvas.fillStyle = "#000"; // Sets colour to black canvas.fillText("Sup Bro!", 50, 50); } </script> </body> </html>