我正在使用Google Charts,使用选择菜单可以看到图表:
<select id="form_frame1" name="frame1" onchange="getChart(this);">
<option value="area_chart_google" >Area Chart</option>
<option value="area_chart_2" selected="selected">Stacked Chart</option>
</select>
我的getChart功能是:
function getChart(selection) {
if (selection.value == "area_chart_2") {
document.getElementById('area_chart_2').style.display = 'block';
document.getElementById('area_chart_google').style.display = 'none';
}
else {
document.getElementById('area_chart_2').style.display = 'none';
document.getElementById('area_chart_google').style.display = 'block';
}
}
使用展示时效果正常:默认情况下为“阻止”(area_chart_google)。问题是,一旦我隐藏(无)图表(area_chart_google)然后我显示它(块)我就会收到此错误:
In Firebug: google-visualization-errors-all-1
In Chrome: Cannot read property 'length' of null
In Safari: 'null' is not an object
这是我的HTML(脚本在正文中,我在标题上使用JSAPI):
<div id="area_chart_google" class="area-chart"></div>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Header');
data.addColumn('number', '');
data.addColumn('number', '');
data.addRows([
['Monday',300,43],
['Tuesday',250,545],
['Wednesday',122,78],
['Thursday',348,92],
['Friday',23,61],
['Saturday',39,93]
]);
var options = {
title: '',
hAxis: {title: '', titleTextStyle: {color: '#efed22'}},
backgroundColor: '#efede9',
legend: 'none'
};
var chart = new google.visualization.AreaChart(document.getElementById('area_chart_google'));
chart.draw(data, options);
}
</script>
答案 0 :(得分:1)
这对我有用:
function getChart(selection) {
if (selection.value == "area_chart_2") {
document.getElementById('area_chart_2').style.display = 'block';
document.getElementById('area_chart_google').style.display = 'none';
}
else {
document.getElementById('area_chart_2').style.display = 'none';
document.getElementById('area_chart_google').style.display = 'block';
drawChart(); //call this function when using this Google Chart
}
}
答案 1 :(得分:0)
在这里猜测,但可能是因为某些脚本动态尝试读取隐藏div的length属性。
如此answer 中所述,display none不会在页面中创建任何空格。您可以尝试一些选项:
Use visibility: hidden
area_chart_2
和area_chart_google
的包装元素答案 2 :(得分:0)
你的问题用jQuery标记,所以这里是一个jQuery解决方案:
function getChart(selection) {
var $area_chart_2 = $("#area_chart_2"),
$area_chart_google = $("#area_chart_google");
if (selection.value === "area_chart_2") {
$area_chart_2.show(); //You could also use $area_chart_2.css("display", "block");
$area_chart_google.hide(); //Or use $area_chart_google.css("display", "none");
} else {
$area_chart_2.hide();
$area_chart_google.show();
}
}