简体   繁体   English

如何获取HTML文档的动态修改SVG元素的字符串?

[英]How to get the string of a dynamically modified SVG element of an HTML document?

I wrote javascript which dynamically edits an svg element. 我编写了动态编辑svg元素的javascript。 I want to get the new string of the edited svg element but currently my code fails. 我想获取已编辑的svg元素的新字符串,但目前我的代码失败了。 I found here the following code to get the string form of an HTML document. 我在这里找到以下代码来获取HTML文档的字符串形式。

var txt = document.documentElement.innerHTML;

I just want the innerHTML of the SVG element. 我只想要SVG元素的innerHTML I tried the following code but get an error message saying "undefined". 我尝试了以下代码,但收到一条错误消息“undefined”。

var svgnode=document.getElementById("svgnode1");
alert(svgnode.innerHTML);

How do I do that. 我怎么做。 The error occurs on Google Chrome but I want a solution that works on any browser. Google Chrome上出现此错误,但我想要一个适用于任何浏览器的解决方案。

innerHTML is defined for HTML documents but not for XML DOMs of other kinds. innerHTML是为HTML文档定义的,但不是为其他类型的XML DOM定义的。

There is some mechanism planned for innerHTML like facilities for SVG. 有一些机制计划用于SVG的innerHTML设施。 http://www.w3.org/Graphics/SVG/WG/wiki/SVG_2_DOM#innerHTML_type_facilities : http://www.w3.org/Graphics/SVG/WG/wiki/SVG_2_DOM#innerHTML_type_facilities

innerHTML type facilities innerHTML类型的设施

It would be good if XML had an innerHTML -like feature. 如果XML具有innerHTML HTML的特性,那将是很好的。 Writing markup in an ECMAScript string can be a bit of a pain, but it can also simplify things a lot, and to get better that that for the types of scenario in which it's useful probably means E4X-like capabilities. 在ECMAScript字符串中编写标记可能有点痛苦,但它也可以简化很多事情,并且对于有用的场景类型可能更好,这可能意味着类似E4X的功能。

Calling this facility innerHTML would make it seem like the markup should be parsed as being in the XHTML namespace, so maybe innerMarkup or insertCode would be a better name, or, for symmetry with textContent , maybe markupContent . 调用这个工具innerHTML会使得标记应该被解析为在XHTML命名空间中,所以也许innerMarkupinsertCode可能是一个更好的名称,或者,对于textContent对称性,可能是markupContent

If what you need is a way to turn SVG, or any other XML DOM into a string of XML, see https://developer.mozilla.org/en/Parsing_and_serializing_XML 如果您需要的是将SVG或任何其他XML DOM转换为XML字符串的方法,请参阅https://developer.mozilla.org/en/Parsing_and_serializing_XML

or see https://stackoverflow.com/a/4916895/20394 for the IE equivalent. 或者参见https://stackoverflow.com/a/4916895/20394获取等效的IE。

Some actual code (Tested in Google-Chrome and IE 11): 一些实际代码(在Google-Chrome和IE 11中测试过):

<object id="sv" data="Switzerland_regions.svg" type="image/svg+xml">
    <!-- <img src="yourfallback.jpg" />-->
</object>


<script type="text/javascript">
        var S = document.getElementById("svgnode1")

        var markup = (new XMLSerializer()).serializeToString(S.contentDocument.rootElement);
        console.log(markup);


        // var SD = S.getSVGDocument();

        // var circle1 = SD.getElementById("GR");
        // console.log(circle1);
        // circle1.style.fill = "grey";

         //var rectangle = SD.querySelector("#layer4");
        //var parent = rectangle.parentNode;
        //parent.removeChild(rectangle);
</script>

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

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