2016-08-11 105 views
0

我試圖以編程方式創建餅圖,希望將其轉化爲React組件以重用。基本上我需要一個可點擊的餅圖,點擊時每個餅圖都會展開成一個整體餅圖。我試圖按照this tutorial製作餅圖並在底部,我有一塊JS,我試圖測試出來。我最終得到了Uncaught Reference Error: $$ is not defined.下面是錯誤消息的屏幕截圖。

enter image description here

我的理解是,這是不是jQuery的和簡直是香草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> 

什麼原因導致錯誤?我誤解了嗎?我跟隨的教程是過時/錯誤的嗎?謝謝!

+1

定義了「$$」的位置? – Rayon

+0

請編輯您的問題,並提供您的代碼als文本 - 不是一個圖像。 – Steve

回答

2

包括以下內容:

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

據筆者,利·貝羅,她提到:

in the book the definition of $$() is given in the introduction, but since this is an excerpt, it doesn’t include that.

+0

哦,謝謝你!這是非常有用的,並修復它。我的預感是正確的:D無論如何,我會接受答案一次SO允許它:) – Sticky

1

tutorial你提到你的問題我得到的東西,將幫助你。

enter image description here

此外,則需要包裝您在code

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

,正如你在標籤註明的jQuery。

希望這會有所幫助。

相關問題