简体   繁体   English

如何更改除clicked元素之外的所有元素?

[英]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

This is the map: 这是地图: 在此输入图像描述

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.

相关问题 Vue.js-更改除单击的元素以外的所有元素的颜色 - Vue.js - change color for all elements except the element clicked 如何删除除单击的元素之外的所有其他元素的类 - How to remove a class on all the other elements except on the element that was clicked 单击窗口/另一个元素时如何更改所有元素的颜色? - How to change the color of all elements when the window/another element is clicked? JavaScript遍历类元素并选择除clicked元素之外的所有元素 - JavaScript iterate through class elements and select all except clicked element 切换除单击的子元素之外的所有元素 - Toggle all elements except the sub element of clicked one 敲除单击绑定-在除clicked元素之外的所有元素上阻止绑定 - knockout click binding - block binding on all elements except clicked element 如何在 JQuery 中选择除单击元素之外的所有类? - how to select all class except the clicked element in JQuery? 定位所有特定元素(点击除外) - Target all the specific elements except the on clicked on 如何删除被点击的元素,而不是删除除了被点击的项目之外的所有项目? - How can I remove the element that is clicked, instead of all items being deleted except the item clicked? 如何选择DOM中除一个元素外的所有元素? - How to select all the elements in the DOM except one element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM