d3 javascript alternate colors on click

前端 未结 3 1884
眼角桃花
眼角桃花 2021-01-04 09:55

I\'m just starting playing around with d3, and was wondering how you could alternate the colors of a element upon clicking it.

This fiddle changes the color of the

3条回答
  •  孤独总比滥情好
    2021-01-04 10:06

    Make yourself a toggle function and pass it into the click: http://jsfiddle.net/maniator/Bvmgm/6/

    var toggleColor = (function(){
       var currentColor = "white";
    
        return function(){
            currentColor = currentColor == "white" ? "magenta" : "white";
            d3.select(this).style("fill", currentColor);
        }
    })();
    

提交回复
热议问题