我使用D3創建條形圖,並使用jquery來選擇條形圖。我遇到了一個問題,我不明白我在哪裏使用$(this)選擇了一個條形(矩形),而console.log()語句似乎證實了這一點。但是,我有問題獲得某些功能(例如轉換,樣式)才能正常工作 - 錯誤消息表示它們不是函數。
我已經構建了我在下面遇到的問題的一個小例子。而不是條形圖,只有兩個矩形。我成功改變了寬度,但不能使用轉換來平滑此更改,也無法使用$(this).style更改填充顏色。
感謝您的幫助!這是我的代碼。
P.S.這是我的第一個StackOverflow問題,所以請讓我知道如果我應該以不同的方式提出這個問題和/或是否有一些我沒有遵循的約定。我在這個問題上花費了幾天的時間進行了相當詳盡的搜索,並且真的遇到了困難。再次感謝!
<body>
<svg width="1000" height="1000" id="this_svg">
<rect x="0" y="0" width="100" height="100" fill="blue" data-color="red"> </rect>
<rect x="0" y="100" width="100" height="100" fill="green" data-color="purple"></rect>
</svg>
<script>
$ (document).ready(function() {
$svg = $("#this_svg")
rcts = $svg.find("rect")
// This Works Fine
rcts.each(function() {
$(this).attr("width", "500")
});
// This breaks (see error message):
// Uncaught TypeError: $(...).transition is not a function
rcts.each(function() {
$(this).transition().duration(500).attr("width", "500")
});
//... and if this hadn't already broken, this next part would
// break instead (see error message):
// Uncaught TypError: $(...).style is not a function
rcts.each(
$(this).style("fill", function() {
return $(this).attr("data-color")
}));
});
</script>
</body>
問:爲什麼你有'd3js'因爲如果你的代碼不使用D3標籤?順便說一句,你得到這個錯誤,因爲'transition()'是一個D3函數,而不是一個jQuery之一。 –