我正在尝试使用jQuery flot创建条形图。我在第一页中包含了所有必需的文件(jQuery)。现在,使用AJAX,我正在加载第二个PHP页面并在DIV中显示内容。 PHP部分的一切工作正常,但JavaScript不起作用。如果我包含所需的文件并在同一页面中编写代码,它工作正常。我已经检查了第二页的所有内容都在第一页的body标签内。
<script src="js/jquery-1.8.3.js" type='text/javascript'></script>
<script type="text/javascript" src="js/flot/jquery.flot.js"></script>
<script type="text/javascript" src="js/flot/jquery.flot.symbol.js"></script>
<script type="text/javascript" src="js/flot/jquery.flot.axislabels.js"></script>
<div id="placeholder" style="width:100%;">
<script type="text/javascript">
var action = document.getElementById('action').value;
var from_date = document.form1.from_date.value;
var to_date = document.form1.to_date.value;
var marketname = document.form1.marketname.value;
var url="lib/reports/SaleItemSales.php?srchtxt="+srchtxt+"&from_date="+from_date+"&to_date="+to_date+"&view="+view+"&page="+page+"&iip="+iip;
xmlhttp.open("POST",url,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==0 || xmlhttp.readyState==1 || xmlhttp.readyState==2 || xmlhttp.readyState==3)
{
document.getElementById('load').style.display="block";
}
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
var resp=xmlhttp.responseText;
document.getElementById('placeholder').innerHTML=resp;
document.getElementById('load').style.display="none";
}
}
</script>
<div style="width:80%;height:500px;text-align:center;margin:10px">
<div id="flot-placeholder" style="width:100%;height:100%;"></div>
</div>
<script language="javascript" type="text/javascript">
var data = [[-0.2, 11],[0.8, 15],[1.8, 25],[2.8, 24],[3.8, 13],[4.8, 18]];
var data2 = [[0.2, 14],[1.2, 17],[2.2, 40],[3.2, 14],[4.2, 19],[5.2, 23]];
var data1 = [[0, 14],[1, 17],[2, 40],[3, 14],[4, 19],[5, 23],[6, 23],[7, 23],[8, 23],[9, 23],[10, 23],[11, 23],[12, 23],[13, 23],[14, 23],[15, 23],[16, 23],[17, 23],[18, 23],[19, 23],[20, 23],[21, 23],[22, 23],[23, 23],[24, 23],[25, 23]];
var dataset = [{ label: "Number of Orders", data: data, color: "#AA0000" },{ label: "Number of Items", data: data1, color: "#00AA00" },{ label: "Amount", data: data2, color: "#0000AA" }];
var ticks = [[0, "19 Jan 2015"], [1, "20 Jan 2015"], [2, "21 Jan 2015"], [3, "22 Jan 2015"],[4, "23 Jan 2015"], [5, "24 Jan 2015"], [6, "19 Jan 2015"],[7, "19 Jan 2015"],[8, "19 Jan 2015"],[9, "19 Jan 2015"],[10, "19 Jan 2015"],[11, "19 Jan 2015"],[12, "19 Jan 2015"],[13, "19 Jan 2015"],[14, "19 Jan 2015"],[15, "19 Jan 2015"],[16, "19 Jan 2015"],[17, "19 Jan 2015"],[18, "19 Jan 2015"],[19, "19 Jan 2015"],[20, "19 Jan 2015"],[21, "19 Jan 2015"],[22, "19 Jan 2015"],[23, "19 Jan 2015"],[24, "19 Jan 2015"],[25, "19 Jan 2015"]];
var options = {
series: {
bars: {show: true},
},
bars: {
align: "center",
barWidth: 0.2
},
xaxis: {
axisLabel: "Date",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 10,
ticks: ticks,
tickLength:0,
},
yaxis: {
axisLabel: "No of Orders, No of Items and Amount",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 3,
tickFormatter: function (v, axis) {
return v + "";
}
},
legend: {
noColumns: 0,
labelBoxBorderColor: "#000000",
position: "nw"
},
grid: {
hoverable: true,
borderWidth: 1,
backgroundColor: { colors: ["#ffffff", "#EDF5FF"] }
}
};
$(document).ready(function (){
$.plot($("#flot-placeholder"), dataset, options);
$("#flot-placeholder").UseTooltip();
});
function gd(year, month, day) {
return new Date(year, month, day).getTime();
}
var previousPoint = null, previousLabel = null;
$.fn.UseTooltip = function () {
$(this).bind("plothover", function (event, pos, item) {
if (item) {
if ((previousLabel != item.series.label) || (previousPoint != item.dataIndex)) {
previousPoint = item.dataIndex;
previousLabel = item.series.label;
$("#tooltip").remove();
var x = item.datapoint[0];
var y = item.datapoint[1];
var color = item.series.color;
//console.log(item.series.xaxis.ticks[x].label);
showTooltip(item.pageX, item.pageY, color,
"<strong>" + item.series.label + "</strong><br>" + item.series.xaxis.ticks[x].label + " : <strong>" + y + "</strong> °C");
}
} else {
$("#tooltip").remove();
previousPoint = null;
}
});
};
function showTooltip(x, y, color, contents) {
$('<div id="tooltip">' + contents + '</div>').css({
position: 'absolute',
display: 'none',
top: y - 40,
left: x - 120,
border: '2px solid ' + color,
padding: '3px',
'font-size': '9px',
'border-radius': '5px',
'background-color': '#fff',
'font-family': 'Verdana, Arial, Helvetica, Tahoma, sans-serif',
opacity: 0.9
}).appendTo("body").fadeIn(200);
}
</script>
答案 0 :(得分:1)
使用innerHTML
插入的JavaScript不会自动执行。您可以使用
document.getElementById('placeholder').innerHTML = '<script type="text/javascript"> alert("test"); </script>';
要使用AJAX加载HTML和JavaScript并执行它,请使用jQuery中的load()方法。在var url = ...
之后用
$('#load').show();
$('#placeholder').load(url, function() {
$('#load').hide();
});