相关代码如下:
function DrawSpecialClass(spclBodyInfList){
      this.ud       = new Object(); 
      this.ud.pgraph  = [ ];  
      this.ud.pdex    = [ ]; 
      this.ud.pindex  = [ ]; 
      this.ud.pvex    = [ ];
      this.ud.pval    = [ ];
      this.ud.mainCanvas; 
      this.ud.mainCt;
}
DrawSpecialClass.prototype.Initpage=function(){

     this.ud.mainCanvas = document.getElementById('canvas_main');
     this.ud.mainCt=this.ud.mainCanvas.getContext('2d');
     this.ud.mainCt.strokeStyle = '#000'; 
     this.ud.mainCt.clearRect(0,0,this.canvasWidth,this.canvasHeight); };var DrawSpl=new DrawSpecialClass(spclBodyInfList);
DrawSpl.Initpage();且已经定义画布如下:
<canvas id="canvas_main" width="1200" height="600" style="border:10px solid #97694F;"></canvas>提示错误:
this.ud.mainCanvas is null
[在此错误处中断] this.ud.mainCt=this.ud.mainCanvas.getContext('2d'); 

解决方案 »

  1.   

    是不是在类中不可以使用  document.getElementById('canvas_main');  来获取画布?
      

  2.   

    没看出来什么错。。不会是要用双引号吧。。http://www.w3schools.com/html5/html5_canvas.asp
      

  3.   

    spclBodyInfList 你这个是什么东西。。
      

  4.   

    是传入的一个数组
    function DrawSpecialClass(spclBodyInfList){
      this.ud = new Object(); 
      this.ud.pgraph = [ ];  
      this.ud.pdex = [ ]; 
      this.ud.pindex = [ ]; 
      this.ud.pvex = [ ];
      this.ud.pval = [ ];
      this.ud.mainCanvas; 
      this.ud.mainCt;  this.spclBodyInfList=spclBodyInfList;
    }
      

  5.   

    现在HTML5很多浏览器都不支持刚用FF测试了下没报错。。你试下。。
      

  6.   

    如果你的javascript代码写在画布前面的话就是因为浏览器在加载javascript脚本的时候并没有加载到画布,因为浏览器是按顺序加载的,并且你的document.getElementByID("canvas_main")是暴露在外面的,加载的时候就执行了,没有找到画布,当然是null了,你可以把脚本放在后面试试