我使用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之一。 –