我有一个美国地图,其中包含可选择的县,点击时将其背景更改为红色。我想要发生的是,当用户点击另一个县时,它会取消选择当前县,然后只选择新县。目前,当点击县级时,它会更改类背景为红色的类,但是当您单击另一个县时,它们都是红色的。如何在使用javascript点击另一课程时取消选择课程
这里就是我绘制地图和变化类点击时的代码:
//DRAW MAP
d3.json("js/map.json", function(error, mapData){
if (error) throw error;
//draw counties
edit.map.append("g")
.selectAll("path")
.data(topojson.feature(mapData, mapData.objects.counties).features)
.enter().append("path")
.attr("class", "counties")
.attr("d", edit.path)
.on("click", function(d){
sFips = d.properties.STATEFP;
cFips = d.properties.COUNTYFP;
//display values in text boxes
$("#locationCountySelect").val(cFips);
$("#locationStateSelect").val(sFips);
//change clicked county class name
if (this.className.baseVal == "counties") {
this.className.baseVal = "selectedCounty";
//send new county to db
} else {
this.className.baseVal = "counties";
}
});
});
同样,我怎么能只在某一时刻选择一个县?
太棒了,谢谢!第一个像魅力一样工作。第二个没有工作。 – lostInTheTetons
请给第二个尝试。我编辑它以避免第一次点击时出现空值。我更喜欢第二种解决方案,因为它比第一种更好。 – altocumulus
第二人也像魅力一样工作,谢谢! – lostInTheTetons