我不是最擅长 javascript,而且我遇到了一些麻烦。任何建议表示赞赏。
我试图在有人单击带有 onclick 事件的链接后生成图表。
我的文件的头部看起来像这样。
<script src="billajax.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="highcharts.js"></script>
<script src="exporting.js"></script>
Billajax.js 有很多功能,但为了简化,它看起来像这样。
function getl(uid){
if(document.getElementById("votescode")){
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse5(xmlHttp.responseText);
//BELOW GENERATES CHART CODE
var xmlHttp2 = getXMLHttp();
xmlHttp2.onreadystatechange = function()
{
if(xmlHttp2.readyState == 4)
{
HandleResponse4(xmlHttp2.responseText);
dothis();
}
}
xmlHttp2.open("GET", "ajax.php?&p=glv&req="+uid+"&n="+Math.random(), true);
xmlHttp2.send(null);
}
}
xmlHttp.open("GET", "ajax.php?&p=gl&req="+uid+"&n="+Math.random(), true);
xmlHttp.send(null);
}else{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse5(xmlHttp.responseText);
//BELOW GENERATES CHART CODE
var xmlHttp2 = getXMLHttp();
xmlHttp2.onreadystatechange = function()
{
if(xmlHttp2.readyState == 4)
{
HandleResponse3(xmlHttp2.responseText);
dothis();
}
}
xmlHttp2.open("GET", "ajax.php?&p=glv&req="+uid+"&n="+Math.random(), true);
xmlHttp2.send(null);
}
}
xmlHttp.open("GET", "ajax.php?&p=gl&req="+uid+"&n="+Math.random(), true);
xmlHttp.send(null);
}
}
function HandleResponse3(response)
{
//var idid = 'waiting';
//toggle_visibility(idid);
var script = document.createElement("script");
script.setAttribute("id", "votescode");
script.innerHTML = response;
document.head.appendChild(script);
}
function HandleResponse4(response)
{
//var idid = 'waiting';
//toggle_visibility(idid);
var el = document.getElementById( 'votescode' );
el.parentNode.removeChild( el );
var script = document.createElement("script");
script.setAttribute("id", "votescode");
script.innerHTML = response;
document.head.appendChild(script);
}
function HandleResponse5(response)
{
//var idid = 'waiting';
//toggle_visibility(idid);
document.getElementById('initiative').innerHTML = response;
//var shn = $('.shtn').ThreeDots();
//shn.ThreeDots.update({max_rows:1});
//var bsy = $('.billsy').ThreeDots();
//bsy.ThreeDots.update({max_rows:3});
}
getL(uid)中ajax调用的输出如下,成功插入到文档头部。使用 HandleResponse5 填充的 html 内容也正确插入。
在我单击链接后,它会触发 getl(uid) 函数,该函数会生成插入页面所需的代码(highcharts 容器),然后它会使用 HandleResponse3 或 Handleresponse4 为 highcharts 生成代码,具体取决于之前是否触发了脚本。
生成高图的代码输出如下。如果我将它扔到 jsfiddle 中它会起作用,所以我无法弄清楚为什么我不断收到以下错误(来自 chrome 控制台)。
Uncaught TypeError: Object [object Object] has no method 'highcharts'
dothis
(anonymous function)
o jquery.min.js:2
p.add jquery.min.js:2
e.fn.e.ready jquery.min.js:2
e.fn.e.init jquery.min.js:2
e jquery.min.js:2
(anonymous function)
HandleResponse3 billajax.js:310
xmlHttp2.onreadystatechange billajax.js:247
Uncaught TypeError: Object [object Object] has no method 'highcharts'
dothis
xmlHttp2.onreadystatechange
这是调用 getl(uid) 中定义的 ajax.php 生成的代码
//CALL FROM BILL AJAX ABOVE WHICH GENERATES CODE BELOW.
xmlHttp2.open("GET", "ajax.php?&p=glv&req="+uid+"&n="+Math.random(), true);
function dothis(){
// Radialize the colors
Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function(color) {
return {
radialGradient: { cx: 0.4, cy: 0.2, r: 0.7
},
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
]
};
});
$('#container').highcharts({
chart: {
renderTo: 'container',
backgroundColor:'transparent',
width:130,height:140,
size:'100%'
},
title: {
text: 'Browser market shares at a specific website, 2010'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
size: 200,
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
credits:{enabled:false},
exporting:{enabled:false},
colors:['#ADD46D','#F1744F','#b9e376','#f2a48d'],
series: [{
type: 'pie',
name: 'Browser share',
data: [
['Supports You', 1],
['Opposes You', 1],
['Absent on Supporting', 0],
['Absent on Opposing', 0]
]
}]
});
}
function dothis2(){
// Radialize the colors
Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function(color) {
return {
radialGradient: { cx: 0.4, cy: 0.2, r: 0.7
},
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
]
};
});
$('#container2').highcharts({
chart: {
renderTo: 'container2',
backgroundColor:'transparent',
width:130,height:140,
size:'100%'
},
legend:{
enabled: false
},
title: {
text: 'Browser market shares at a specific website, 2010'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
size: 200,
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
credits:{enabled:false},
exporting:{enabled:false},
colors:['#ADD46D','#F1744F','#b9e376','#f2a48d'],
series: [{
type: 'pie',
name: 'Browser share',
data: [
['Supports You', 0],
['Opposes You', 2],
['Absent on Supporting', 0],
['Absent on Opposing', 0]
]
}]
});
}
$(function () { dothis();dothis2(); });
我已经成功实现了一个类似的解决方案,但我无法弄清楚为什么当我使用类似的设置时它不能正常工作。
再次,任何建议将不胜感激。