getSVGDocument在FireFox和Chrome中返回null
getSVGDocument returns null in FireFox and Chrome
getSVGDocument坏了吗?过时了吗?
因为当我"运行"以下内容时:
<!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
整页(含文件)可在此下载:https://github.com/stopsatgreen/modernwebbook/blob/master/Code%20Examples/Chapter%2007/svg-embedded.html
注意:我会尝试让页面在JS Bin中运行。
编辑:
问题是这样的吗?在本地主机上访问时SVG不工作。为什么?
如果是这样,如何修复它不配置(本地)web服务器?
不推荐使用getSVGDocument()
方法。建议使用contentDocument
属性。
看到
找到了如何解决这个问题,这是一个与安全相关的问题。
Chrome首先关闭所有运行的chrome实例。然后用命令行标志启动Chrome可执行文件:
铬——allow-file-access-from-files
在Windows上,最简单的可能是创建一个添加了标志的特殊快捷方式(右键单击快捷方式-> properties -> target)。
Firefox转到about:config找到security.fileuri。strict_origin_policy参数设置为false
明白了:https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally
相关文章:
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- php函数的ajax html$_POST值返回null
- 是否可以 document.getElementsByTagName('head')[0] ever 返回 null
- Firebase:查询返回null
- 在浅层渲染的反应组件上,Enzyme为道具返回null
- jQuery Multiselect插件返回null长度(应为0?)
- 如何处理getElementById返回Null
- JavaScript中的RegExp返回null
- Javascript 中的 RegEx 返回 Null
- 加载时的getelementbyID返回null
- 谷歌脚本在第二次调用Gmail后返回null
- Indexeddb OpenCursor在特定键值上返回null
- Ajax响应返回null
- 有没有一种方法可以判断图像src是否在javascript内部返回null
- 当JS在Chrome控制台上正常工作时,Selenium JavaScript执行器返回null
- Auth$getUser返回null,即使用户已登录(AngularFire,Firebase)
- Mongoose JS findOne总是返回null
- 为什么我的ajax调用返回null
- 当与全局标志一起使用时,Javascript Regex在后续调用中返回null
- Regex以确定该值在JS中是否有效,返回null