将参数传递给'document.getElementById'

时间:2019-09-18 12:34:50

标签: javascript html charts google-visualization

我正在尝试将div id作为参数传递给document.getElementById()

我尝试使用以下功能,但仍然无法正常工作。

function getID(Element_ID) {
        var el = document.getElementById(Element_ID);
        el.style.visibility = "visible";
        if(el.style.display == "none" ) {
            el.style.display = "inline";
        }
        else {
            el.style.display = "visible";
        }
        return el;//in case you want to use the element
}

代码:

<html>
   <head>
      <title>Google Charts Tutorial</title>
      <script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js"></script>
      <script type="text/javascript" src="https://www.google.com/jsapi"></script>


      <script type = "text/javascript">
         google.charts.load('current', {packages: ['corechart']});     
      </script>
       <script language = "JavaScript">
         function drawChart(DivID){
            // Define the chart to be drawn.
            var data = google.visualization.arrayToDataTable([
               ['Status', 'Outcome', { role: 'style' }],
               ['Executed - 70', 70,'green'],
               ['Not Executed 5', 5,'red']
            ]);
    var groupData = google.visualization.data.group(
    data,
    [{column: 0, modifier: function () {return 'total'}, type:'string'}],
    [{column: 1, aggregation: google.visualization.data.sum, type: 'number'}]
  );

  var formatPercent = new google.visualization.NumberFormat({
    pattern: '#,##0.0%'
  });

  var formatShort = new google.visualization.NumberFormat({
    pattern: 'short'
  });

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1, 2,{
    calc: function (dt, row) {
      var amount =  formatShort.formatValue(dt.getValue(row, 1));
      var percent = formatPercent.formatValue(dt.getValue(row, 1) / groupData.getValue(0, 1));
      return amount + ' (' + percent + ')';
    },
    type: 'string',
    role: 'annotation'
  }]);

        var options = { title: 'Coveo (700341)', 'legend':'none'}; 

        var chart = new google.visualization.ColumnChart(document.getElementById(DivID));
        chart.draw(view, options);
     }
     google.charts.setOnLoadCallback(drawChart);
      </script>
   </head>

   <body>
 <table border='1px' cellpadding='5' cellspacing='0' style='border: solid 1px Silver; font-size: x-small;width: 100%'>
 <tr align='center' valign='top'>
<td align='center' style='width:100px' valign='center' bgcolor='white'>
    <div id="container1" style="width:400; height:280"></div>
    <script type="text/javascript">
   drawChart('container1');
</script>
</td>
 </tr>
</table>
   </body>
</html>

当我将document.getElementById(DivID)更改为document.getElementById('container1')时有效。还尝试将google.charts.setOnLoadCallback(drawChart)更改为google.charts.setOnLoadCallback(drawChart('container1'))。根本没有运气。

1 个答案:

答案 0 :(得分:2)

回调函数将不传递任何参数,
改用匿名函数...

google.charts.setOnLoadCallback(function () {
  drawChart('container1');
});