fab*_*ien 3 javascript jquery svg d3.js
我有像这样的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>
Run Code Online (Sandbox Code Playgroud)
我想做那样的somtehing:
foreach(path in #countries){
if(this.class === 3){dostuff}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过jquery方式:
$('#countries').find('path').each(function( index ) {
console.log(this);
});
Run Code Online (Sandbox Code Playgroud)
......什么都不记录
我尝试过d3.js方式(各国初始化如下:)countries = svg.append('svg:g').attr('id', 'countries')
:
countries.select('path').each(console.log(this));
countries.selectAll('path').each(console.log(this));
Run Code Online (Sandbox Code Playgroud)
记录窗口对象(wtf?)
nau*_*tat 17
方法selection.each需要一个函数作为参数:
countries.selectAll('path').each(function(d,i) { console.log(this); });
Run Code Online (Sandbox Code Playgroud)