svg必需放在一个div容器里
var svgHeadInfo = '<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"\n"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">'; var svg = $('#vizContainer').find('svg')[0]; svg.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); var svgSource = d3.select("svg") .attr("version", 1.1) .attr("xmlns", "http://www.w3.org/2000/svg") .node().parentNode.innerHTML; d3.select("body").append("a") .attr("title", "file.svg") .attr("href-lang", "image/svg+xml") .attr("href", "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgHeadInfo + svgSource)))) .text("Download");