[英]How to change all elements except clicked element?
I'm working on a world map visualization using Javascript and D3.js. 我正在使用Javascript和D3.js进行世界地图可视化。 When you click on a country all other countries get a class
inactiveCountry
which adds a opacity: 0.3
, which highlights the selected country. 当您单击某个国家/地区时,所有其他国家/地区都会获得一个
inactiveCountry
的类,该类会添加opacity: 0.3
,突出显示所选国家/地区。 What I would like is that when you click the highlighted country again, all other countries lose the class inactiveCountry
so they have a opacity: 1
again. 我想要的是,当您再次单击突出显示的国家/地区时,所有其他国家/地区都会失去类
inactiveCountry
因此它们的opacity: 1
。
When selecting a country: 选择国家时:
The following is the code that draws and colors the map: 以下是绘制和着色地图的代码:
g.selectAll("path").data(countries.features) .enter().append("path") .attr("d", pathGenerator) .attr("class", "country") .attr("id", function (d){ return d.properties.name }) .attr("fill", function (d) { return colorScaleWorldmap(colorValue(d)) }) .on("click", function (d) { changeCountry(d.properties.name); }) .append("title") .text(function (d) { return d.properties.name + ": " + colorValue(d); });
As you can see, when you click on a country it runs the function changeCountry();
如您所见,当您单击某个国家/地区时,它会运行函数
changeCountry();
, which is this: ,这是:
function changeCountry(countryName) { country = countryName; console.log(country); let totalCountries = document.getElementsByClassName("country"); // Remove class countryActive from previous selection for (let i = 0; i < totalCountries.length; i++) { if (totalCountries[i].classList.contains("countryActive")) { totalCountries[i].classList.replace("countryActive", "countryInactive") } } // Add class countryActive to selected country let element = document.getElementById(country); element.classList.add("countryActive"); for (let i = 0; i < totalCountries.length; i++) { if (totalCountries[i].classList.contains("countryActive")) { totalCountries[i].classList.remove("countryInactive") } else { totalCountries[i].classList.add("countryInactive") } } }
How would I go about adding an interaction where when you click the selected country again , all countries become visible (thus losing the class countryInactive
)? 我将如何添加交互,当您再次单击所选国家/地区时 ,所有国家/地区都会变为可见(从而失去类
countryInactive
)? I've tried using a JQuery .click
and .data
combination as such: 我尝试过使用JQuery
.click
和.data
组合:
$("#" + country).click(function(){ $(this).data('clicked', true); }); if($("#" + country).data('clicked')) { alert('yes'); }
The problem with this piece of code is that once I've clicked a country at least once, it seems to keep the true
statement, so even when I click it while it's inactive, I get the yes
alert. 这段代码的问题在于,一旦我点击了一个国家至少一次,它似乎保留了
true
陈述,所以即使我在它处于非活动状态时点击它,我也会得到yes
警告。
Try this code: 试试这段代码:
function changeCountry(countryName) {
let clickedContry = document.getElementById(countryName);
let totalCountries = document.getElementsByClassName("country");
if(clickedContry.classList.contains("countryActive")) {
for (let i = 0; i < totalCountries.length; i++) {
if (totalCountries[i].classList.contains("countryInactive")) {
totalCountries[i].classList.replace("countryInactive", "countryActive");
}
}
} else {
// Remove class countryActive from previous selection
clickedContry.classList.add("countryActive");
}
}
. 。
But remember.. you still didn't think about what will happen when you have a clicked country and the user click in another. 但请记住..你仍然没有考虑当你点击一个国家而用户点击另一个国家时会发生什么。
Good Luck! 祝好运!
With the help of the solution thiagotrss provided I managed to solve it myself. 在thiagotrss提供的解决方案的帮助下,我设法自己解决了这个问题。 This is how I did it:
这就是我做的方式:
function changeCountry(countryName) { let totalCountries = document.getElementsByClassName("country"); let clickedCountry = document.getElementById(countryName); country = countryName; console.log(country); if (clickedCountry.classList.contains("countryActive")) { clickedCountry.classList.remove("countryActive") country = "Total countries"; for (let i = 0; i < totalCountries.length; i++) { if (totalCountries[i].classList.contains("countryInactive")) { totalCountries[i].classList.remove("countryActive") totalCountries[i].classList.remove("countryInactive") } } } else { for (let i = 0; i < totalCountries.length; i++) { if (totalCountries[i].classList.contains("countryActive")) { totalCountries[i].classList.remove("countryActive") } else if (!totalCountries[i].classList.contains("countryActive")) { totalCountries[i].classList.add("countryInactive") } } clickedCountry.classList.add("countryActive") } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.