0

正如您在下面看到的,我创建了函数 getChart(); 它在自己调用时有效,但在我将其包装到 $(document).ready(function(){...}); 时不显示图表;我还附上了文件..任何帮助都受到高度重视和赞赏..thanx..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Google Visualization API Sample</title>
  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">

    function getChart(){
    google.load('visualization', '1', {packages: ['corechart']});
    function drawVisualization() {
       // Populate the data table.

        var dataTable = google.visualization.arrayToDataTable([
           ['Mon', 20, 28, 38, 45],
           ['Fri', 68, 66, 22, 15]
        // Treat first row as data as well.
        ], true);   

        // Draw the chart.
        var chart = new     google.visualization.CandlestickChart(document.getElementById('visualization'));
        chart.draw(dataTable, {legend:'none', width:600, height:400});
    }

    google.setOnLoadCallback(drawVisualization);

    }

    $(document).ready(function(){
        $('#idbutton').click(function(){
            getChart();
        });
    });


  </script>
</head>
<body>
<input id="idbutton" type="button" value="button" />
<div id="visualization"></div>
</body>
</html>
4

1 回答 1

0

尝试将其附加到窗口对象。就像是

$(document).ready(function(){
    window.getChart = function() { //Code of getChart here };
});

并调用它:

$(document).ready(function(){
    $('#idbutton').click(function(){
        window.getChart();
    });
});

注意:我没有测试它。

于 2012-08-02T20:00:01.807 回答