Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript d3js:单击元素后如何切换css类_Javascript_D3.js - Fatal编程技术网

Javascript d3js:单击元素后如何切换css类

Javascript d3js:单击元素后如何切换css类,javascript,d3.js,Javascript,D3.js,我绘制了一张热图,与这个d3js示例完全相同 我如何修改代码,如果我点击一个方块,它会打开/关闭该方块的特定CSS类?非常简单——这样更改代码: var heatMap = svg.selectAll(".hour") .data(data) .enter().append("rect") .attr("x", function(d) { return (d.hour - 1) * gridSize; }) .att

我绘制了一张热图,与这个d3js示例完全相同

我如何修改代码,如果我点击一个方块,它会打开/关闭该方块的特定CSS类?

非常简单——这样更改代码:

var heatMap = svg.selectAll(".hour")
          .data(data)
          .enter().append("rect")
          .attr("x", function(d) { return (d.hour - 1) * gridSize; })
          .attr("y", function(d) { return (d.day - 1) * gridSize; })
          .attr("rx", 4)
          .attr("ry", 4)
          .attr("class", "hour bordered")
          .attr("width", gridSize)
          .attr("height", gridSize)
          .style("fill", colors[0])
          .on("click", function() {
            d3.select(this).classed("myCssClass", d3.select(this).classed("myCssClass") ? false : true);
          });

甚至更短的点击功能:
d3.select(this).classed(“myCssClass”),!d3.select(this).classed(“myCssClass”)