【发布时间】: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