Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将SVG DOM节点漂亮地打印到文件_Javascript_Svg_Xml Parsing_Blob - Fatal编程技术网

Javascript 将SVG DOM节点漂亮地打印到文件

Javascript 将SVG DOM节点漂亮地打印到文件,javascript,svg,xml-parsing,blob,Javascript,Svg,Xml Parsing,Blob,我使用Javascript允许用户将动态生成的SVG元素导出到文件中。使用blob和MouseEvents是一种相当粗糙的方法: var exportSVG=函数(sel){ var el=文档查询选择器(sel); var blob=new blob([el.outerHTML],{编码:“UTF-8”,类型:“text/xml;charset=UTF-8”}), e=document.createEvent('MouseEvents'), a=document.createElement('

我使用Javascript允许用户将动态生成的SVG元素导出到文件中。使用blob和MouseEvents是一种相当粗糙的方法:

var exportSVG=函数(sel){
var el=文档查询选择器(sel);
var blob=new blob([el.outerHTML],{编码:“UTF-8”,类型:“text/xml;charset=UTF-8”}),
e=document.createEvent('MouseEvents'),
a=document.createElement('a');
a、 下载=”;
a、 href=window.URL.createObjectURL(blob);
a、 dataset.downloadurl=['text/xml',a.download,a.href].join(':');
e、 initMouseEvent('click',true,false,window,0,0,0,false,false,false,0,null);
a、 调度事件(e);
};
exportSVG(“mysvg”);
问题是这个SVG的源在一行上。其中一些单行SVG在我的浏览器中无法正确渲染(字母混乱)-如果它们打印得很好,就可以了

所以我想用正确的源代码格式和缩进保存它们


我想我必须改变这个Blob hack,因为它本质上使源代码成为一行。对于XML是否有类似于
JSON.stringify(el,null,2)
的东西?

对这个帮助有什么答案吗?这个问题的答案有帮助吗?