我正在用raphaeljs創建一堆圖表,並且我希望在頁面加載後將它們添加到特定div的內容中。使用jQuery將raphael生成的圖表添加到文檔中
這裏是我的html:
<div class="chartdiv">
<span class="A">35</span>
<div class="charthere"></div>
</div>
<div class="chartdiv">
<span class="A">25</span>
<div class="charthere"></div>
</div>
<div class="chartdiv">
<span class="A">15</span>
<div class="charthere"></div>
</div>
下面的jQuery代碼應該通過與類chartdiv,併爲每個
- 所有div環與A級的跨度中獲得的價值,
- 將它傳遞給drawChart功能
- 把SVG在div與類charthere
這裏我的JS腳本的相關部分:
$(document).ready(function(){
$('div.chartdiv').each(function(){
// get the A value
var A = parseInt($(this).add('span.A').text());
// substitute the html with the corresponding chart
drawChart($(this).add('div.charthere'), A);
});
function drawChart(element, A) {
var paper = new Raphael(element, 200, 200);
var examplePathString = "M20,100 L" + A + ",100";
var thePath = paper.path(examplePathString);
thePath.attr({stroke: red,'stroke-width': 5px});
}
});
反而會發生什麼情況是,所有的SVG元素的div後創建。
感謝您指出了這一點。通過查看console.log,我能夠通過遵循您的建議並使用.children('span.A').text()而不是.add('span.A ')。文本()。實例化Raphael對象時,我也刪除了'new'。 –
仍然帶drawChart($(this).children('div.charthere'),A);它看起來像看着控制檯,傳遞的元素是div的(空)內容而不是div本身。 –
這讓我意識到還有第二個問題 - 請參閱上面的編輯 – user568458