javascript - jQuery 美元符号未定义

标签 javascript jquery google-visualization yql

为了获取历史信息,我尝试使用以下代码。 Chrome 调试器提示 Uncaught ReferenceError: $ is not defined。你能建议一个修复方法吗,我真的被困住了。我只需要它在 Chrome 上工作,我正在利用 YQL 和 Yahoo API。

这是 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>

最佳答案

只需在脚本之前添加对 JQuery 源的引用:

<script src="http://code.jquery.com/jquery.min.js"></script>

关于javascript - jQuery 美元符号未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13482022/

相关文章:

javascript - ng-google-chart 柱形图中每列的自定义颜色

javascript - 更改页面加载 ionic 上的导航栏颜色

javascript - 将 JSON 对象反序列化为其原始类

JavaScript 文本范围错误

jquery - 如何居中对齐div(与text-align : center)?相同的方式

javascript - 基于打开/关闭属性的 HTML Detail 标签高度调整

javascript - 如何将引导按钮与输入复选框链接起来?

javascript - amChart 和 React js(版本 15)(es6)

javascript - Google 图表中的工具提示 : Timeline API

javascript - Google 图表中的分页?