javascript jQuery 美元符号未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13482022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery dollar sign is undefined
提问by bouncingHippo
In my quest to get historical information, i tried using the below code. Chrome debugger says that Uncaught ReferenceError: $ is not defined
. Can you suggest a fix, i'm really stuck. I just need this to work on Chrome, and I am tapping into YQL and Yahoo API.
为了获取历史信息,我尝试使用以下代码。Chrome 调试器说Uncaught ReferenceError: $ is not defined
。你能建议修复吗,我真的被卡住了。我只需要它在 Chrome 上工作,并且我正在使用 YQL 和 Yahoo API。
here is the jsFiddle http://jsfiddle.net/pCK5q/1/
这是 jsFiddle http://jsfiddle.net/pCK5q/1/
<html>
<head>
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages':['annotatedtimeline']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
/* historical data code that breaks */
var url = 'http://query.yahooapis.com/v1/public/yql';
var startDate = '2012-01-01';
var endDate = '2012-01-08';
var jsonData = encodeURIComponent('select * from yahoo.finance.historicaldata where symbol in ("YHOO","AAPL","GOOG","MSFT") and startDate = "' + startDate + '" and endDate = "' + endDate + '"');
$.getJSON(url, 'q=' + data + "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json", callback);
data.addColumn('date', 'Date');
data.addColumn('number', 'Sold Pencils');
data.addColumn('string', 'title1');
data.addColumn('string', 'text1');
data.addColumn('number', 'Sold Pens');
data.addColumn('string', 'title2');
data.addColumn('string', 'text2'); // not on the fly
data.addRows([
[new Date(2008, 1 ,1), 30000, undefined, undefined, 40645, undefined, undefined],
[new Date(2008, 1 ,2), 14045, undefined, undefined, 20374, undefined, undefined],
[new Date(2008, 1 ,3), 55022, undefined, undefined, 50766, undefined, undefined],
[new Date(2008, 1 ,4), 75284, undefined, undefined, 14334, 'Out of Stock','Ran out of stock on pens at 4pm'],
[new Date(2008, 1 ,5), 41476, 'Bought Pens','Bought 200k pens', 66467, undefined, undefined],
[new Date(2008, 1 ,6), 33322, undefined, undefined, 39463, undefined, undefined]
]);
var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
chart.draw(data, {displayAnnotations: true});
}
</script>
</head>
<body>
// Note how you must specify the size of the container element explicitly!
<div id='chart_div' style='width: 700px; height: 240px;'></div>
</body>
采纳答案by Jeff B.
Add this line:
添加这一行:
google.load("jquery", "1.7.1");
Just under this one you already have:
就在此之下,您已经拥有:
google.load('visualization', '1', {'packages':['annotatedtimeline']});
This will load jQuery from the Google jsapi you already use. This is the best solution regarding your code.
这将从您已经使用的 Google jsapi 加载 jQuery。这是关于您的代码的最佳解决方案。
回答by McGarnagle
Just add a reference to the JQuery source before your script:
只需在脚本之前添加对 JQuery 源的引用:
<script src="http://code.jquery.com/jquery.min.js"></script>
回答by SpYk3HH
Try adding a jQuery reference:
尝试添加一个 jQuery 引用:
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.min.js'></script>
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>
OR (but i thinks this wont work because you are calling a jQuery in your loading function, thus first way is probably better)
或(但我认为这行不通,因为您在加载函数中调用了 jQuery,因此第一种方法可能更好)
Add it via the JSAPI Google Loader you're using:
通过您正在使用的 JSAPI Google Loader 添加它:
<head>
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages':['annotatedtimeline']});
google.load("jquery", "1.8.3"); // note, you can also load jQueryUI this way,
// on another note, not sure how high a version google supports
Please see Hosted Libsfor more info
// 另一方面,不确定谷歌支持的版本有多高
请参阅托管库了解更多信息