简体   繁体   中英

d3js - Create a server side SVG with javascript actions

I'm trying to enable javascript code to be invoked on an action bounded to an SVG element created in NodeJS using d3@3. Unfortunately, the on('click') is not rendered.

router.get('/test', function(req, res) {
    const dom = new JSDOM();
    var svg = d3.select(dom.window.document.body)
        .append("svg")
        .attr("xmlns", "http://www.w3.org/2000/svg")

    svg.insert('rect') 
        .attr("width", 200)
        .attr("height", 200)
        .on('click', function() { console.log('click')});

    svgStr = dom.window.document.body.innerHTML;
    res.set('Content-Type', 'image/svg+xml');
    res.send(svgStr);
})

Output:

<svg xmlns="http://www.w3.org/2000/svg"><rect width="200" height="200"> 
</rect></svg>

Where's the on('click') ??

set

`
//...
.attr("height", 200)
.attr("onclick" , "console.log('click')")`

for you click

and remember actions is not allowed from images to page

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