admin管理员组文章数量:1321445
I'm following a javascript/coffeescript/canvas tutorial, I have this javascript code:
(function() {
$(function() {
var canvas, context;
console.log("DOM is ready");
canvas = $('#myCanvas');
context = canvas.getContext('2d');
context.font = '40pt Calibri';
context.fillStyle = 'blue';
return context.fillText('Hello World!', 150, 100);
});
}).call(this);
on calling canvas.getContext()
, I'm getting an Uncaught TypeError: undefined is not a function.
If I replace canvas = $('#myCanvas');
with document.getElementById('myCanvas');
it works fine.
What do you think is the problem? Thank you!!
For info, this is my HTML:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo</title>
<script src="jquery-1.11.1.js"></script>
<script src="test.js"></script>
</head>
<body>
<canvas id="myCanvas" width="578" height="200"></canvas>
</body>
</html>
And my original Coffeescript:
$ ->
console.log("DOM is ready")
canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d')
context.font = '40pt Calibri';
context.fillStyle = 'blue';
context.fillText('Hello World!', 150, 100);
I'm following a javascript/coffeescript/canvas tutorial, I have this javascript code:
(function() {
$(function() {
var canvas, context;
console.log("DOM is ready");
canvas = $('#myCanvas');
context = canvas.getContext('2d');
context.font = '40pt Calibri';
context.fillStyle = 'blue';
return context.fillText('Hello World!', 150, 100);
});
}).call(this);
on calling canvas.getContext()
, I'm getting an Uncaught TypeError: undefined is not a function.
If I replace canvas = $('#myCanvas');
with document.getElementById('myCanvas');
it works fine.
What do you think is the problem? Thank you!!
For info, this is my HTML:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo</title>
<script src="jquery-1.11.1.js"></script>
<script src="test.js"></script>
</head>
<body>
<canvas id="myCanvas" width="578" height="200"></canvas>
</body>
</html>
And my original Coffeescript:
$ ->
console.log("DOM is ready")
canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d')
context.font = '40pt Calibri';
context.fillStyle = 'blue';
context.fillText('Hello World!', 150, 100);
Share
Improve this question
asked Jun 15, 2014 at 19:26
H-HH-H
4561 gold badge9 silver badges16 bronze badges
1 Answer
Reset to default 10The difference is that $('#myCanvas') returns jQuery object and document.getElementById('myCanvas') returns canvas html element. To get canvas context you need the element not an object. If you want use jQuery just change "canvas = $('#myCanvas');" to "canvas = $('#myCanvas')[0];"
本文标签: javascriptUncaught TypeError undefined is not a function with jQueryStack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: undefined is not a function with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742100378a2420774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论