下面的代碼給出「Uncaught TypeError:不能調用未定義的方法'getContext'」。該代碼是我的實驗,使用here所述的模塊模式來移植畫布應用。Javascript錯誤;而在本地嘗試
請在jsfiddle找到相同的代碼,它工作正常。
謝謝。
//====test.html ========
<html>
<head>
<title>Location</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<canvas id="cvs" height="600" width="800"></canvas>
</body>
</html>
//==test.js ========
if (typeof (NS) === 'undefined') {
NS = {};
}
NS.AppName = (function ($) {
// Private members
/*var _first = function() {
},
_second = function() {
},*/
var _cvs = $("#cvs");
var _ctx = _cvs.get(0).getContext("2d");
var _privateVar = "privateVar: accessed from within AppLaunch.Admin namespace";
// Private Methods
var _privateMethod = function() {
log("privateMethod: accessed only from within AppLaunch.Admin");
}; // Last variable in a chain must always end with ; before the return {}
function log() {
if (window.console && window.console.log)
window.console.log('[AppName] ' + Array.prototype.join.call(arguments, ' '));
};
return {
init: function() {
},
// Public
myPublicMethod: function() {
//alert("myPublicMethod" + _cvs.height());
_ctx.fillRect(25,25,100,100);
_ctx.clearRect(45,45,60,60);
_ctx.strokeRect(50,50,50,50);
}
}
})(jQuery);
// Initialize
jQuery().ready(function() {
NS.AppName.init()
NS.AppName.myPublicMethod();
});
感謝您的詳細說明... – bsr 2011-05-01 15:13:14