[英]How to get innerHTML from one specify child ELEMENT
All: 所有:
What I want to do is: 我想做的是:
<div id="container">
<div class="unwanted"></div>
<div class="unwanted"></div>
<svg><rect></rect></svg>
<div class="unwanted"></div>
<div>
How can I get the innerHTML of #container for that svg, so what I get is: 我如何获取该svg的#container的innerHTML,所以我得到的是:
"<svg><rect></rect></svg>"
What you want is the outerHTML of the svg Element but that is not really accessible in this way. 您想要的是svg元素的externalHTML,但实际上不能以这种方式访问。 Here
这里
outerHTML of an SVG element SVG元素的externalHTML
is an idea of how to get the svg with content. 是关于如何获取内容的svg的想法。
在这种情况下,这将为您提供所需的东西:
document.getElementById('container').getElementsByTagName('svg')[0].innerHTML;
jquery jQuery的
var container = $("#container").html().find('svg');
// or
$("#container svg").html();
//return <rect></rect> you can use '<svg>'+container+'</svg>'
javascript JavaScript的
var container = document.getElementById('container');
var svg = container.getElementsByTagName('svg')[0].innerHTML;
//return <rect></rect> you can use '<svg>'+container+'</svg>'
or see @Duopixel's answer on Why .html() doesn't work with SVG selectors using jquery ? 或参阅@Duopixel关于为什么.html()无法与使用jquery的SVG选择器一起使用的答案?
var svg = document.getElementById('svg_root'); // or whatever you call it
var serializer = new XMLSerializer();
var str = serializer.serializeToString(svg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.