【问题标题】:Uncaught ReferenceError: $$ is not defined未捕获的 ReferenceError:$$ 未定义
【发布时间】:2016-12-17 17:29:18
【问题描述】:

我正在尝试以编程方式创建一个饼图,希望将其变成一个 React 组件以供重复使用。基本上我需要一个可点击的饼图,点击时每个切片都会展开成一个完整的饼图。我正在尝试跟随this tutorial 制作饼图,在底部,我有一块我试图测试的JS。我最终得到了Uncaught Reference Error: $$ is not defined. 这是错误消息的屏幕截图。

我的理解是,这不是 jQuery,而只是 Vanilla JS。我不确定这是否属实。我通过 CDN 导入了 jQuery,但仍然遇到同样的错误。我读了这个SO post,我在想$$ 只是一种变量名表示法。

这是我在index.html 中的代码,没有什么突破性的。

<body>
  <div class="pie">20%</div>
  <div class="pie">60%</div>
  <script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
  <script type="text/javascript">
    $$('.pie').forEach(function(pie) {
      var p = parseFloat(pie.textContent);
      var NS = "http://www.w3.org/2000/svg";
      var svg = document.createElementNS(NS, "svg");
      var circle = document.createElementNS(NS, "circle");
      var title = document.createElementNS(NS, "title");
      circle.setAttribute("r", 16);
      circle.setAttribute("cx", 16);
      circle.setAttribute("cy", 16);
      circle.setAttribute("stroke-dasharray", p + " 100");
      svg.setAttribute("viewBox", "0 0 32 32");
      title.textContent = pie.textContent;
      pie.textContent = '';
      svg.appendChild(title);
      svg.appendChild(circle);
      pie.appendChild(svg);
    });
  </script>
</body>

是什么导致了错误?我是不是误会了?我正在关注的教程是否过时/错误?谢谢!

【问题讨论】:

  • $$ 定义在哪里?
  • 请编辑您的问题并提供您的代码作为文本而不是图像。

标签: javascript jquery css svg


【解决方案1】:

您在问题中提到的 tutorial 我得到了一些可以帮助您的东西。

另外,你需要包装你的code

$(function(){....});

正如您在标签中提到的 jquery。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您是否尝试过在 $$('.pie') 上使用单 $ 而不是双 $$,所以您的代码类似于 $('.pie')

    并检查您是否已经包含 jQuery

    这是有用的链接

    http://www.w3schools.com/jquery/jquery_ref_selectors.asp

    https://api.jquery.com/

    【讨论】:

      【解决方案3】:

      包括以下内容:

      function $$(selector, context) {
        context = context || document;
        var elements = context.querySelectorAll(selector);
        return Array.prototype.slice.call(elements);
      }
      

      据作者 Lea Verou 所说,她提到:

      本书的引言中给出了$$()的定义,但由于这是摘录,因此不包括在内。

      【讨论】:

      • 哦,谢谢!这非常有用并修复了它。我的预感是对的 :D 无论如何,一旦 SO 允许,我会接受作为答案:)
      猜你喜欢
      • 2023-01-23
      • 2016-11-03
      • 2011-01-05
      • 2016-01-02
      • 2013-10-06
      • 1970-01-01
      相关资源
      最近更新 更多