[英]Google JSAPI function can not be accessed
I am trying to move this piece of Google chart example code to 我正在尝试将这段Google图表示例代码移至
$(document).ready(function () {});
Right now it is placed in the script tag right after the Google JSAPI script and it works, like so: 现在,它被放置在Google JSAPI脚本之后的script标记中,并且它可以正常工作,如下所示:
<!--Google JSAPI-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: question,
backgroundColor: '#CFCFCF'
};
var chart = new google.visualization.PieChart(document.getElementById('SS_piechart'));
chart.draw(data, options);
}
</script>
This is in my page_tail, just before closing body tag. 这是在我的page_tail中,在关闭body标签之前。 But as soon as I put the code inside the
但是,只要我将代码放入
$(document).ready(function () {});
I get 我懂了
Uncaught TypeError: google.visualization.arrayToDataTable is not a function
未捕获的TypeError:google.visualization.arrayToDataTable不是函数
<!--Google JSAPI-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
$(document).ready(function () {
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: question,
backgroundColor: '#CFCFCF'
};
var chart = new google.visualization.PieChart(document.getElementById('SS_piechart'));
chart.draw(data, options);
}
});
</script>
I have some other code sitting along in the $(document).ready function, is it possible I have code conflict, or is there something obvious I am missing? 我在$(document).ready函数中还有其他代码,是否可能发生代码冲突,或者我显然缺少什么?
It is possible to move the callback 可以移动回调
google.setOnLoadCallback(drawChart);
to 至
$(document).ready(function () {
drawChart();
});
or JS native 或JS原生
document.addEventListener("DOMContentLoaded", drawChart);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.