4

次のような d3.js によって生成された svg があります。

<svg height="1094" width="1254">
    <g id="countries" stroke-width="1px" transform="">
        <path d="M100,519.150,200,250" fill="rgba(100,100,100,0.8)"
        stroke="rgba(200,200,200,0.5)" id="id1" class="1"></path>
        <path d="M100,519.150,200,250" fill="rgba(100,100,100,0.8)"
        stroke="rgba(200,200,200,0.5)" id="id2" class="1"></path>
        <path d="M100,519.150,200,250" fill="rgba(100,100,100,0.8)"
        stroke="rgba(200,200,200,0.5)" id="id3" class="3"></path>
        <path d="M100,519.150,200,250" fill="rgba(100,100,100,0.8)"
        stroke="rgba(200,200,200,0.5)" id="id4" class="1"></path>
    </g>
</svg>

私はそのようなことをしたい:

foreach(path in #countries){
    if(this.class === 3){dostuff}
}

私はjqueryの方法を試しました:

$('#countries').find('path').each(function( index ) {
    console.log(this);
});

... 何も記録しない

私はd3.jsの方法を試しました(国は次のように初期化されました:) countries = svg.append('svg:g').attr('id', 'countries'):

countries.select('path').each(console.log(this));
countries.selectAll('path').each(console.log(this));

ウィンドウ オブジェクトをログに記録します (wtf?)

4

1 に答える 1

19

メソッドselection.eachには、引数として関数が必要です。

countries.selectAll('path').each(function(d,i) { console.log(this); });
于 2013-01-26T01:52:44.807 に答える