javascript getSVGDocument 在 Chrome 中返回 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16626938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:24:39  来源:igfitidea点击:

getSVGDocument is returning NULL in Chrome

javascriptjquerysvg

提问by user2383880

I'm starting to use SVG and I have the following code:

我开始使用 SVG 并且我有以下代码:

HTML

HTML

<embed id="embed" src="svg.svg" type="image/svg+xml" width="400" height="300" >

<object id="object" data="svg.svg" type="image/svg+xml" width="400" height="300"></object>

Javascript

Javascript

$(window).load(function (){
//alert("Document loaded, including graphics and embedded documents (like SVG)");

    var svgDoc_embed = document.getElementById("embed").getSVGDocument();
    alert("My svgDoc_embed => " + svgDoc_embed);

    var svgDoc_object = document.getElementById("object").getSVGDocument();
    alert("My svgDoc_object => " + svgDoc_object);  

});

In the FireFox browser works well

在 FireFox 浏览器中运行良好

My svgDoc_embed => [object SVGDocument]
My svgDoc_object => [object SVGDocument]

but does not work on Chrome browser.

但不适用于 Chrome 浏览器。

My svgDoc_embed => null
My svgDoc_object => null

I have searched the Internet but can not find anything that works

我已经在互联网上搜索过,但找不到任何有效的东西

Any suggestion is welcome

欢迎任何建议

==========================================================================

================================================== ========================

I try opening the Chrome JS console and type in:

我尝试打开 Chrome JS 控制台并输入:

document.getElementById ("object"). GetSVGDocument ();

document.getElementById ("object"). GetSVGDocument ();

The result is null.

结果为空。

Also change the code to:

还将代码更改为:

$ ("embed"). load (function () {
svgDoc_embed var = document.getElementById ("embed"). getSVGDocument ();
alert ("My # embed svgDoc_embed =>" + svgDoc_embed);
});

  $ ("object"). load (function () {
  svgDoc_object var = document.getElementById ("object"). getSVGDocument ();
alert ("My # object svgDoc_object =>" + svgDoc_object);
  });

And does not give any results.

并且不给出任何结果。

Any suggestion is welcome.

欢迎任何建议。

回答by Paul LeBeau

I believe getSVGDocument() is deprecated and didn't work in my copy of FF. Try using firstElementChild instead:

我相信 getSVGDocument() 已被弃用并且在我的 FF 副本中不起作用。尝试使用 firstElementChild 代替:

var svg = document.getElementById("object").firstElementChild;

or access the SVG element directly with:

或直接访问 SVG 元素:

var svg = document.getElementById("foo");

Where "foo" is the value of the id attribute of the root svg element.

其中“foo”是根 svg 元素的 id 属性值。

<svg id="foo">