【问题标题】:jQuery dollar sign is undefinedjQuery美元符号未定义
【发布时间】:2012-11-20 20:59:07
【问题描述】:

为了获取历史信息,我尝试使用以下代码。 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 参考在哪里?
  • 你在导入jquery吗?或其他使用 $? 的库
  • 从 JsFiddle 左侧的下拉列表中选择“jQuery”。目前是 Mootools...
  • 请学习如何使用jsFiddle:jsfiddle.net/pCK5q/2。此外,您永远不会在任何地方定义 callback
  • @FelixKling,那我应该如何定义callback

标签: javascript jquery google-visualization yql


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    添加这一行:

    google.load("jquery", "1.7.1");
    

    就在这个你已经拥有的下面:

    google.load('visualization', '1', {'packages':['annotatedtimeline']});
    

    这将从您已经使用的 Google jsapi 加载 jQuery。这是关于您的代码的最佳解决方案。

    【讨论】:

    • @bouncingHippo:嗯,错误很明显不是吗?您在$.getJSON(...) 中使用它,但您从未定义它。我们不是调试服务...
    • callback 是 getJSON 完成后发生的情况,将其替换为 function(){alert("done!");}
    • @Jean-FrançoisG.B.我收到了一个错误的请求GET http://query.yahooapis.com/v1/public/yql?q=[object%20Object]&amp;env=http%3A%2F%2Fdatatables.org%2Falltables.env&amp;format=json 400 (Bad Request) jquery.min.js:4 send jquery.min.js:4 f.extend.ajax jquery.min.js:4 f.(anonymous function) jquery.min.js:4 f.extend.getJSON jquery.min.js:4 drawChart
    • 这是另一个与您寻求帮助的问题无关的问题。您尝试从中获取信息的 URL 不起作用。你应该这样调查……
    • @Jean-FrançoisG.B.我在 url 中发现了错误并修复了它。谢谢!
    【解决方案3】:

    尝试添加一个 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'>
    

    或者(但我认为这行不通,因为您在加载函数中调用了 jQuery,因此第一种方法可能更好)

    通过您正在使用的 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,
    

    // 另一个说明,不确定 google 支持多高的版本
    请参阅Hosted Libs 了解更多信息

    【讨论】:

    • 有趣的方法...它会阻止bad GET request吗?
    • 如果你先加载 jQuery 库,那么 $.getJSON 将工作,只要它的参数设置正确,虽然老实说,我还没有看过你的,现在......呃,我不知道那条数据线,我通常将数据作为对象发送,而不是字符串,但试试看! See example here
    猜你喜欢
    • 2010-12-14
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 2021-07-01
    • 1970-01-01
    • 2022-11-22
    • 2011-11-26
    相关资源
    最近更新 更多