簡體   English   中英

如何從$(document).ready()運行javascript函數

[英]How to run javascript function from $(document).ready()

我正在使用jsgantt庫在網頁上創建甘特圖。 問題如下。

變體1不起作用 ,而變體2正在起作用 顯示警報“開始”,而在功能createChartControl未顯示警報“ Fin”。 因此,我無法弄清為什么Variant 1無法正常運行(即空白的DIV容器)並且Firebug不顯示任何錯誤消息。 非常感謝您的幫助。

變體1:

    <script>
    function createChartControl(htmlDiv)
    {

alert("Start");

          var g = new JSGantt.GanttChart('g',document.getElementById(htmlDiv), 'hour');

          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)
          g.setFormatArr("hours","minutes")

          if( g ) {

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

            // You can also use the XML file parser JSGantt.parseXML('project.xml',g)

            g.AddTaskItem(new JSGantt.TaskItem(1,   'Add minutes/hours',            '',                 '',                 'ff0000', 'http://help.com',        0, 'Ilan',     0, 1, 0, 1));
            g.AddTaskItem(new JSGantt.TaskItem(11,  'Add support for half days',    '5/14/2009 14:00',  '5/14/2009 15:30',  'ff00ff', 'http://www.jsgantt.com', 0, 'Ilan',  100, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(12,  'Add minute view',              '5/14/2009 16:00',  '5/14/2009 17:00',  '00ff00', '',                       0, 'Ilan',   40, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(13,  'Add hours view',               '5/14/2009 16:00',  '5/14/2009 17:00',  '00ffff', 'http://www.yahoo.com',   0, 'Ilan', 60, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(14,  'Add support for format options',               '5/14/2009 18:00',  '5/14/2009 19:00',  '00ffff', 'http://www.yahoo.com',   0, 'Shlomy', 60, 0, 14, 1));


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

alert("Fin");

          }
          else
          {
            alert("not defined");
          }
    }
    </script>

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

    });
    </script>

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

變體2:

<div style="position:relative" class="gantt" id="schedule"></div>
<script>
      var g = new JSGantt.GanttChart('g',document.getElementById('schedule'), 'hour');

      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)
      g.setFormatArr("hours","minutes")

      if( g ) {

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

        // You can also use the XML file parser JSGantt.parseXML('project.xml',g)

        g.AddTaskItem(new JSGantt.TaskItem(1,   'Add minutes/hours',            '',                 '',                 'ff0000', 'http://help.com',        0, 'Ilan',     0, 1, 0, 1));
        g.AddTaskItem(new JSGantt.TaskItem(11,  'Add support for half days',    '5/14/2009 14:00',  '5/14/2009 15:30',  'ff00ff', 'http://www.jsgantt.com', 0, 'Ilan',  100, 0, 1, 1));
        g.AddTaskItem(new JSGantt.TaskItem(12,  'Add minute view',              '5/14/2009 16:00',  '5/14/2009 17:00',  '00ff00', '',                       0, 'Ilan',   40, 0, 1, 1));
        g.AddTaskItem(new JSGantt.TaskItem(13,  'Add hours view',               '5/14/2009 16:00',  '5/14/2009 17:00',  '00ffff', 'http://www.yahoo.com',   0, 'Ilan', 60, 0, 1, 1));
        g.AddTaskItem(new JSGantt.TaskItem(14,  'Add support for format options',               '5/14/2009 18:00',  '5/14/2009 19:00',  '00ffff', 'http://www.yahoo.com',   0, 'Shlomy', 60, 0, 14, 1));


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

      }

      else

      {

        alert("not defined");

      }
</script>

更新1:我應該說,當我使用dhtmlxGantt庫在函數createChartControl中創建圖表時,變體1正在工作。

更新2:我在變體1中更新了示例代碼。仍然無法正常工作-即顯示警報“開始”,而未顯示警報“ Fin”。

您確定從updateList獲得的響應是​​JSON對象嗎?

echo json_encode(array('a'=>'b'));
die();

如果您不響應JSON對象,請使用$ .get()方法。

PS:請不要使用'updateList.php?query ='+'DROP DATABASE()'嘗試一些MVC邏輯。

jsgantt需要全局變量g才能起作用。 var g更改為window.g
所以看起來像這樣

變體1:

<script>
    function createChartControl(htmlDiv)
    {
        alert("Start");

        window.g = new JSGantt.GanttChart('g',document.getElementById(htmlDiv), 'hour');

        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)
        g.setFormatArr("hours","minutes")

        if(g) {
            // Parameters (pID, pName, pStart, pEnd, pColor, pLink, pMile, pRes, pComp, pGroup, pParent, pOpen)
            // You can also use the XML file parser JSGantt.parseXML('project.xml',g)

            g.AddTaskItem(new JSGantt.TaskItem(1, 'Add minutes/hours', '', '', 'ff0000', 'http://help.com', 0, 'Ilan', 0, 1, 0, 1));
            g.AddTaskItem(new JSGantt.TaskItem(11, 'Add support for half days', '5/14/2009 14:00', '5/14/2009 15:30', 'ff00ff', 'http://www.jsgantt.com', 0, 'Ilan', 100, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(12, 'Add minute view', '5/14/2009 16:00', '5/14/2009 17:00', '00ff00', '', 0, 'Ilan',  40, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(13, 'Add hours view', '5/14/2009 16:00', '5/14/2009 17:00', '00ffff', 'http://www.yahoo.com',  0, 'Ilan', 60, 0, 1, 1));
            g.AddTaskItem(new JSGantt.TaskItem(14, 'Add support for format options', '5/14/2009 18:00', '5/14/2009 19:00', '00ffff', 'http://www.yahoo.com',  0, 'Shlomy', 60, 0, 14, 1));

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

            alert("Fin");
        }
        else {
            alert("not defined");
        }
    }
</script>

<script>
    $(document).ready(function() {
        createChartControl('schedule');
    });
</script>
<div style="position:relative" class="gantt" id="schedule"></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM