getSVGDocument在FireFox和Chrome中返回null [英] getSVGDocument returns null in FireFox and Chrome

查看:272
本文介绍了getSVGDocument在FireFox和Chrome中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

getSVGDocument是否已损坏?已过时?

Is getSVGDocument broken? Obsolete?

因为当我运行以下内容时:

Because when I "run" the following:

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>SVG Embedded - Chapter 07</title>
  <link rel="stylesheet" href="../bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../assets/style.css">
  <style>
    body { margin: 1em; }
    svg { border: 1px solid silver; }
    rect, text { fill: white; }
    circle { fill: black; }
  </style>
</head>
<body>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" height="200" width="300">
<rect width="100%" height="100%" fill="black" />
<circle cx="150" cy="100" r="80" fill="white" />
<text x="150" y="125" font-size="60" text-anchor="middle">SVG</text>
</svg>
<embed src="../assets/svg.svg"></embed>
<object data="../assets/svg.svg"></object>
<script>
document.addEventListener('DOMContentLoaded',function() {
  'use strict';
var rects = document.querySelectorAll('svg'),
      embed = document.querySelector('embed').getSVGDocument();
console.log(rects.length);
  console.log(embed,embed.childNodes.length);
},false);
</script>
</body>
</html>

然后等待所有3个SVG显示(已加载和) ,并在控制台内输入: document.getElementsByTagName('embed')[0] .getSVGDocument()它返回null

then wait for all the 3 SVG to be (loaded and) displayed, and input inside the console: document.getElementsByTagName('embed')[0].getSVGDocument() it returns null

可以在那里下载整页(带文件):
https://github.com/stopsatgreen/modernwebbook/blob/master/Code%20Examples/Chapter%2007/svg-embedded.html

Full page (with files) can be downloaded there: https://github.com/stopsatgreen/modernwebbook/blob/master/Code%20Examples/Chapter%2007/svg-embedded.html

注意:我会尝试在JS Bin中运行页面。

Note: I'll try to have the page running in JS Bin.

编辑:

问题可能是这个吗? SVG在localhost上访问时无法正常工作。为什么?

Could the problem be this? SVG not working when access on localhost. Why?

如果是这样,如何在不配置(本地)网络服务器的情况下修复它?

If so, how to fix it without configuring a (local) webserver?

推荐答案

不推荐使用 getSVGDocument()方法。推荐的方法是使用 contentDocument 属性。

The getSVGDocument() method is deprecated. The recommended way is to use the contentDocument attribute instead.

请看这里

这篇关于getSVGDocument在FireFox和Chrome中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆