0

我使用$(document).ready(). 问题是 CATCH 被执行并且err.message返回的g 没有定义。但alert(g)返回对象 Object。我不明白为什么对象g被如此奇怪地删除了。请帮忙。

http://jsfiddle.net/Udkkv/4/

 <script>
function createChartControl(htmlDiv) {
      var g = new JSGantt.GanttChart('g',document.getElementById(htmlDiv), 'day');

        g.setShowRes(1); // Show/Hide Responsible (0/1)
        g.setShowDur(1); // Show/Hide Duration (0/1)
        g.setShowComp(1); // Show/Hide % Complete(0/1)
       g.setCaptionType('Resource');  // Set to Show Caption (None,Caption,Resource,Duration,Complete)

        // Parameters             (pID, pName,                  pStart,      pEnd,        pColor,   pLink,          pMile, pRes,  pComp, pGroup, pParent, pOpen, pDepend, pCaption)

        g.AddTaskItem(new JSGantt.TaskItem(1,   'Define Chart API',     '',          '',          'ff0000', 'http://help.com', 0, 'Brian',     0, 1, 0, 1));
        g.AddTaskItem(new JSGantt.TaskItem(11,  'Chart Object',         '7/20/2008', '7/20/2008', 'ff00ff', 'http://www.yahoo.com', 1, 'Shlomy',  100, 0, 1, 1));
        g.AddTaskItem(new JSGantt.TaskItem(12,  'Task Objects',         '',          '',          '00ff00', '', 0, 'Shlomy',   40, 1, 1, 1));
        g.AddTaskItem(new JSGantt.TaskItem(121, 'Constructor Proc',     '7/21/2008', '8/9/2008',  '00ffff', 'http://www.yahoo.com', 0, 'Brian T.', 60, 0, 12, 1));
        g.AddTaskItem(new JSGantt.TaskItem(122, 'Task Variables',       '8/6/2008',  '8/11/2008', 'ff0000', 'http://help.com', 0, 'Brian',         60, 0, 12, 1,121));
        g.AddTaskItem(new JSGantt.TaskItem(123, 'Task by Minute/Hour',       '8/6/2008',  '8/11/2008 12:00', 'ffff00', 'http://help.com', 0, 'Ilan',         60, 0, 12, 1,121));
        g.AddTaskItem(new JSGantt.TaskItem(124, 'Task Functions',       '8/9/2008',  '8/29/2008', 'ff0000', 'http://help.com', 0, 'Anyone',   60, 0, 12, 1, 0, 'This is another caption'));
        g.AddTaskItem(new JSGantt.TaskItem(2,   'Create HTML Shell',    '8/24/2008', '8/25/2008', 'ffff00', 'http://help.com', 0, 'Brian',    20, 0, 0, 1,122)); 

        g.Draw();   
        g.DrawDependencies();

      }

}
</script>

<script>
$(document).ready(function() {             
   createChartControl('schedule');
});

<div style="position:relative" class="gantt" id="schedule"></div>

更新:我应该说,如果我在没有 $(document).ready() 的情况下运行此代码 - 即在 DIV 标签之后,那么它可以工作。怎么会这样???

更新 2:这里是复制项目: http: //jsfiddle.net/Udkkv/4/它不工作,但也许现在它会更容易帮助。

4

1 回答 1

1

var g 在定义之前使用。问题出在您网站上的 .js 文件中:“http://www.jsgantt.com/jsgantt.js”。

if (vGroup != 1)
  {  
     vStart = JSGantt.parseDateStr(pStart,g.getDateInputFormat());
     vEnd   = JSGantt.parseDateStr(pEnd,g.getDateInputFormat());
  }

在上面的代码中,您尝试使用:

g.getDateInputFormat()

而 var g 仍未定义。

于 2012-08-11T21:57:26.617 回答