简体   繁体   English

如何提取javascript生成的DOM对象的html代码?

[英]How can I extract the html code for a DOM object generated by javascript?

I am generating a svg object in a html document using java script. 我正在使用java脚本在html文档中生成一个svg对象。 Something like this: 像这样的东西:

mySvg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
myPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
mySvg.appendChild(myPath);

Is there a javascript command to extract what the resulting html code would be? 是否有一个javascript命令来提取生成的HTML代码是什么?

ie

"<svg>...<path>...</path>...</svg>"

I want to then save this part as a string variable. 我想将此部分保存为字符串变量。

Thanks, Wendy 谢谢,温迪

Yeah...Put your svg tag into a div and then get the Html of that div. 是啊...将你的svg标签放入div中,然后获取该div的Html。

<div id="container"></div>
$('#container').append('mySvg');

And for the whole svg as an html, you can do this: 对于作为html的整个svg,你可以这样做:

var svgHtml = $('#container').html();

For the string: 对于字符串:

var svgHtml = document.getElementById('container').innerText;

You can use the innerHTML to get the html text for an element 您可以使用innerHTML获取元素的html文本

mySvg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
myPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
mySvg.appendChild(myPath);

document.getElementById('x').appendChild(mySvg);

console.log(document.getElementById('x').innerHTML)

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM