简体   繁体   English

使用给定的ID访问特定的路径元素

[英]Access a specific path element using given ID

I am using Topojson and world-110m.json to visualize a map of the world. 我正在使用Topojson和world-110m.json来可视化世界地图。 I am trying to change the fill property of two particular countries by click event. 我试图通过点击事件更改两个特定国家/地区的填充属性。

The first country is selected by click from the user side and the ID of that path is retrieved using: 通过从用户端单击选择第一个国家,并使用以下方法检索该路径的ID:

var current_country = d3.select(this).style("fill", "red");
var current_country_ID = d3.select(this).attr('id');

The second country to be changed is given (does not really matter) and defined by ID. 给出要更改的第二个国家(并不重要),并由ID定义。 I have tried using this: 我试过使用此:

var top = d3.select("path#643").style("fill", "green");

As suggested here: How to select a d3 svg path with a particular ID 如此处建议: 如何选择具有特定ID的d3 svg路径

Seems pretty straight forward but I always get the same error no matter what I try: 似乎很简单,但是无论我尝试什么,我总是会遇到相同的错误:

Uncaught SyntaxError: Failed to execute 'querySelector' on 'Document': 'path#643' is not a valid selector. 未捕获到的SyntaxError:无法在“文档”上执行“ querySelector”:“ path#643”不是有效的选择器。

I have tried many things (all combinations that come to my mind) and I have seen a lot of similar questions posted here but have failed to find the proper solution. 我尝试了很多事情(想到的所有组合),并且在这里看到了很多类似的问题,但是未能找到合适的解决方案。 A path with that ID does in fact exist. 实际上存在具有该ID的路径。 And all of the countries are stored in the world variable and they seem OK to me. 而且所有国家/地区都存储在world变量中,对我来说似乎还可以。

Here is my full code: 这是我的完整代码:

    var width = 2000;
    var height = 2000;

    var svg = d3.select("body").append("svg")
              .attr("width", width) 
              .attr("height", height);

    var projection = d3.geo.mercator()
              .center([50,70]) 
              .scale(150) 
              .rotate([0,0,0]); 

    var path = d3.geo.path().projection(projection); 

    var g = svg.append("g"); 

    var country_selector = d3.select("body").append("div").attr("class", "country_selector"); 

    queue() 
      .defer(d3.json, "world-110m.json")
      .defer(d3.csv, "country_data2.csv")
      .await(main);

    function main(error, world, countryData){

        var country_names = {}; 
        var countries = topojson.feature(world, world.objects.countries).features;

        countryData.forEach(function(d) {
        country_names[d.id] = d.name; 
        });

        var world =  g.selectAll("path") 
                    .data(countries)
                    .enter()
                    .append("svg:path")
                    .attr("d", path)
                    .attr("id", function(d) { return d.id; })
                    .attr("class", function(d) { return d.id; })
                    .on("mouseover", function(d) {
                    country_selector.text(country_names[d.id])
                    .style("left", (d3.event.pageX + 7) + "px")
                    .style("top", (d3.event.pageY - 15) + "px")
                    .style("display", "block")
                    .style("opacity", 0.8);
                    })
                    .on("mouseout", function(d) {
                    country_selector.style("opacity", 0)
                    .style("display", "none");
                    })
                    .on("mousemove", function(d) {
                    country_selector.style("left", (d3.event.pageX + 7) + "px")
                    .style("top", (d3.event.pageY - 15) + "px");
                    })
                    .on("click", function(d) { // the rouble part
                    var current_country = d3.select(this).style("fill", "red")
                    var current_country_ID = d3.select(this).attr('id')
                    console.log(current_country)
                    console.log(current_country_ID)
                    console.log(world)
                    var top = d3.select("path#643").style("fill", "green"); // the bad guy
                    console.log(top)
                    })  
    }; // end of main function

    var zooming = d3.behavior.zoom() 
                .on("zoom",function() {  
                    g.attr("transform","translate("+ 
                        d3.event.translate.join(",")+")scale("+d3.event.scale+")");
                    g.selectAll("path")  
                        .attr("d", path.projection(projection)); 
                });

    svg.call(zooming).on("dblclick.zoom", null);

Any help would be greatly appreciated. 任何帮助将不胜感激。

id s can't start with numbers. id不能以数字开头。 So the id itself is invalid. 因此, id本身无效。 You can change the id in the HTML to something like _643 , then in your JavaScript do 您可以将HTML中的id更改为_643类的_643 ,然后在JavaScript中执行

var top = d3.select("path#_643").style("fill", "green");

Here's an example using CSS to show id validity 这是一个使用CSS显示id有效性的示例

 #643 { background-color: orange; color: #FFF; } #_643 { background-color: orange; color: #FFF; } #-643 { background-color: orange; color: #FFF; } #six43 { background-color: orange; color: #FFF; } 
 <ul> <li id="643">643</li> <li id="_643">_643</li> <li id="-643">-643</li> <li id="six43">six43</li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM