简体   繁体   中英

d3.select by namespaced attribute

This almost does it for my case:

d3.select('g[id^="layer"]')

But it's just not bulletproof. I need to be able to do:

d3.select('g[inkscape:groupmode="layer"]')

This produces an invalid selector error. Doesn't make a difference whether I add inkscape to d3.namespaces or not.

Ps I'm working on an Inkscape edited SVG, I need to be able to render it in browser DOM, manipulate it and export it back to an SVG with all "metadata" in namespaced attributes intact.

I ended up filtering using Element.getAttributeNS .

// Set namespace
d3.namespaces.inkscape = 'http://www.inkscape.org/namespaces/inkscape'

// Filter selection
d3.select('#my_svg').selectAll('g').filter(function (d, i) {
    return this.getAttributeNS(d3.namespaces.inkscape, 'groupmode') === 'layer'
})

Does anyone know of a more D3-like way of doing it?

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