1

大家,我在原始的 flot.js 库中遇到了这个错误。我想知道这部分脚本有什么问题?*错误是 shwon Fire bug from Firefox 错误消息是:

ReferenceError: $ is not defined

$(document).ready(function () { 

开始声称是错误代码的行是:

$(document).ready(function () {
$.plot($("#flot-placeholder1"), dataset, options);
$("#flot-placeholder1").UseTooltip();
}); 

以下是错误行之前的内容:

 <script>
//******* 2012 Gold Price Chart
var data1 = [
[gd(2012, 0, 1), 1652.21], [gd(2012, 1, 1), 1742.14], [gd(2012, 2, 1), 1673.77], [gd(2012, 3, 1), 1649.69],
[gd(2012, 4, 1), 1591.19], [gd(2012, 5, 1), 1598.76], [gd(2012, 6, 1), 1589.90], [gd(2012, 7, 1), 1630.31],
[gd(2012, 8, 1), 1744.81], [gd(2012, 9, 1), 1746.58], [gd(2012, 10, 1), 1721.64], [gd(2012, 11, 2), 1684.76]
];
var data2 = [
[gd(2012, 0, 1), 0.63], [gd(2012, 1, 1), 5.44], [gd(2012, 2, 1), -3.92], [gd(2012, 3, 1), -1.44],
[gd(2012, 4, 1), -3.55], [gd(2012, 5, 1), 0.48], [gd(2012, 6, 1), -0.55], [gd(2012, 7, 1), 2.54],
[gd(2012, 8, 1), 7.02], [gd(2012, 9, 1), 0.10], [gd(2012, 10, 1), -1.43], [gd(2012, 11, 2), -2.14]
];
var dataset = [
{ label: "Gold Price", data: data1, points: { symbol: "triangle"} },
{ label: "Change", data: data2, yaxis: 2 }
];
var options = {
series: {
lines: {
show: true
},
points: {
radius: 3,
fill: true,
show: true
}
},
xaxis: {
mode: "time",
tickSize: [1, "month"],
tickLength: 0,
axisLabel: "2012",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 10
},
yaxes: [{
axisLabel: "Gold Price(USD)",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 3,
tickFormatter: function (v, axis) {
return $.formatNumber(v, { format: "#,###", locale: "us" });
}
}, {
position: "right",
axisLabel: "Change(%)",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 3
}
],
legend: {
noColumns: 0,
labelBoxBorderColor: "#000000",
position: "nw"
},
grid: {
hoverable: true,
borderWidth: 2,
borderColor: "#633200",
backgroundColor: { colors: ["#ffffff", "#EDF5FF"] }
},
colors: ["#FF0000", "#0022FF"]
};

=================更新=============================== =====================大家好。感谢你的回答!这是我加载脚本的顺序(加载正确,因为我在 DW CS4 1.jquery-1.8.3.min.js 2.jquery.flot.min.js 2.jquery.flot.min.js" 3.jquery.flot.time.js" 4. jquery.flot.symbol.js 5. jquery.flot.axislabels.js 6. jshashtable-2.1.js 7. jquery.numberformatter-1.2.3.min.js

所以我认为顺序应该不是问题,它按照教程告诉我在这里做: http ://www.pureexample.com/jquery-flot-tutorial-how-to-make-a-jquery-flot-line -chart.html

我还发现这些代码被 Firefox/firebug 阻止了,也许它有帮助。停止位于代码的第二行之前--> '$(this).bind("plot...'.

$.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;
var month = new Date(x).getMonth();
//console.log(item);
if (item.seriesIndex == 0) {
showTooltip(item.pageX,
item.pageY,
color,
"<strong>" + item.series.label + "</strong><br>" + monthNames[month] + " : <strong>" + y + "</strong>(USD)");
} else {
showTooltip(item.pageX,
item.pageY,
color,
"<strong>" + item.series.label + "</strong><br>" + monthNames[month] + " : <strong>" + y + "</strong>(%)");
}
}
} else {
$("#tooltip").remove();
previousPoint = null;
}
});
}; 

谢谢。

对凯文的进一步问题在评论下询问他的回答......

4

2 回答 2

3

错误在这里:

$(document)

$没有定义。这意味着您要么不包含 jQuery,要么包含它太晚,要么使用$.noConflict()不正确。

更具体地说,$ === undefined和 undefined 不是函数,因此无法执行。

于 2013-04-18T18:57:23.483 回答
3

这意味着您需要在 HTML 页面中包含 jQuery。在运行您的$(document).ready行之前添加这个(或对 jQuery 库的不同引用):

<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.min.js"></script>

$指的是 jQuery 库,需要在使用前可用。

另外,你需要在加载jQuery库之后再加载flot库。flot 是一个 jQuery 插件,所以它依赖于 jQuery 库。

于 2013-04-18T18:57:28.473 回答