当我使用jquery ajax函数.load()时,这个插件不起作用,如何解决这个问题?这是我的 .php 代码:
<script src="../Chart.js"></script>
<script type="text/javascript" src="../functions.js"></script>
<script src="../jquery-2.1.4.js"></script>
<link rel="stylesheet" href="../jquery-ui.min.css">
<script src="../external/jquery/jquery.js"></script>
<script src="../jquery-ui.min.js"></script>
<canvas id="canvas" width="400" height="400"></canvas>
And this is my functions.js:
jQuery(document).ready(function() {
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var lineChartData = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
label: "My First dataset",
fillColor : "rgba(220,220,220,0.2)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(220,220,220,1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
label: "My Second dataset",
fillColor : "rgba(151,187,205,0.2)",
strokeColor : "rgba(151,187,205,1)",
pointColor : "rgba(151,187,205,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(151,187,205,1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
]
}
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true
});
}
});
为什么它不起作用?该代码来自该插件包中的示例!那么它怎么可能不起作用呢?我认为这是因为使用了 jquery 中名为 .load() 的 AJAX 函数。如何解决这个问题呢?请帮忙。
最佳答案
这是您的代码的工作示例,我已经删除了“onload”分配,因为您已经有一个运行整个过程的文档就绪事件。并移出了不需要的功能和数据。看一下,如果有不明白的地方请告诉我
window.onload = function(){ //is removed
https://jsfiddle.net/L7zgL02w/2/
而且,您应该在其他文件之前加载 jquery.js(因为您的functions.js 使用 jquery)
关于javascript - ChartJS 无法在 jquery .load() 加载的 .php 中工作(AJAX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34666998/