【问题标题】:getSVGDocument is returning NULL in Chrome [duplicate]getSVGDocument 在 Chrome 中返回 NULL [重复]
【发布时间】:2013-05-18 17:16:08
【问题描述】:

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

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

$(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);  

});

在 FireFox 浏览器中运行良好

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

但不适用于 Chrome 浏览器。

My svgDoc_embed => null
My svgDoc_object => null

我已经搜索了互联网,但找不到任何有效的方法

欢迎提出建议

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

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

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

结果为空。

同样将代码改为:

$ ("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);
  });

并且没有给出任何结果。

欢迎提出任何建议。

【问题讨论】:

  • contentDocument 能否在 Chrome 中工作?即 document.getElementById("embed").contentDocument
  • getSVGDocument() 在我的一台服务器上的 Chrome 上工作,但在另一台服务器上却不行。也许是密码保护? contentDocument 也返回 null。 firstElementChild 也为空。
  • 显然你必须先等待它加载,然后contentDocument 似乎工作。见stackoverflow.com/questions/11434916/…
  • 我在尝试从本地文件系统而不是 http 或 https 使用 GetSVGDocument 时遇到了同样的问题。这似乎是一个安全问题。请参阅stackoverflow.com/questions/52693026 和 Serge 在stackoverflow.com/questions/22529398 中的回答

标签: javascript jquery svg


【解决方案1】:

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

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

或直接访问 SVG 元素:

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

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

<svg id="foo">

【讨论】:

  • 当您插入 元素内联时,它不会形成单独的 SVG 文档。它成为外部 HTML 文档的一部分。然而,当 SVG 嵌入 时,它包含在它自己的文档中。
  • 我找不到关于 getSVGDocument() 被弃用的任何信息。另外,目前大多数浏览器都使用supported
猜你喜欢
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 2017-01-05
  • 2011-02-06
  • 1970-01-01
  • 2019-10-04
相关资源
最近更新 更多