gpt4 book ai didi

javascript - Wordpress 中的谷歌图表

转载 作者:行者123 更新时间:2023-11-30 13:22:36 27 4
gpt4 key购买 nike

我想在 Wordpress 页面中显示 Google Graph。

我学习了 here Wordpress 似乎可以使用 Javascript。

所以我创建了一个包含基本代码的页面(这是 GCharts 的示例)。我的页面看起来像:

    <!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {

// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);

// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div id="chart_div"></div>

不幸的是,我的网站上没有显示任何内容。

有人知道发生了什么事吗?

谢谢

编辑:

我发现 Wordpress 添加了 <[CDATA[ 标签。在我的代码中,此标记可能被“]”字符破坏,这会导致问题。

这是 Firebug 信息:

syntax error


google.setOnLoadCallback(drawChart);</p>

?page_id=22 (line 88, col 43)

不在我的代码中!!

我注意到,当我在页面中点击“更新”时,代码变为:

<script type="text/javascript">// <![CDATA[
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {

// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);

// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
// ]]></script>

最佳答案

解决方案是......不要在页面的 javascript 代码中使用空行

关于javascript - Wordpress 中的谷歌图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9682278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com