简体   繁体   中英

Selecting SVG with D3

I have the following function:

function myfunc(d) {
  var svg = d3.select('.map-wrap svg');
  console.log('svg is:');
  console.log(svg);

  // interesting stuff happens later ...
}

I call that function on a mouseover event. Here is code where myfunc is supposed to be called.

myChart.width(800)
    .height(800)
    .dimension(xDim)
    .group(xDimGrp)
    .projection(projection)
    .colors(quantize)
  .colorDomain(quantize.domain())
  .colorCalculator(function (d) { return d ? getColorClass(myChart.colors()(d)) : '#ccc'; })
    .overlayGeoJson(map.features, 'states', function (d) {
        return d.properties.state;
    }).on('mouseover', myfunc);

When I print out svg I expect to see this:

数组中的svg对象

instead, I see the following:

svg为空

I see 0: null instead of 0: svg , why is this happening? How can I select the SVG in a way that will give me what is shown in first picture?

.map-wrap is like so:

地图包装div

就像Cyril所说的那样,在更好地调试之后,我意识到在创建svg元素之前确实已经调用了它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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