SVG 符号不会触发加载事件(仅在 Firefox 中).我寻找解决方法
SVG symbols do not fire onload event (only in firefox). I look for a workaround
我正在一个项目中使用 D3.js 使用 SVG 制作图表。该图使用 SVG 符号。我有javascript代码来管理带有onload事件的这些项目。
在诸如webkit引擎之类的浏览器中,浏览器运行得非常好。但是火狐...Firefox 不会在 SVG 中运行任何加载事件。
好吧,我看到了这个错误,并认为Firefox是自由软件,这些人喜欢错误跟踪。我已经上传了错误 https://bugzilla.mozilla.org/show_bug.cgi?id=1254159。
这些人的反应是"不会修复"。
检查错误的示例很小且清晰:
<html>
<head>
<title>test onload event in a svg symbol</title>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" pointer-events="all" width="500px" height="500px" style="background: #ffff00;">
<g class="cat" transform="translate(100 100)">
<use xlink:href="animals.svg#cat" onload="javascript: console.log('This message is not showed in firefox.');" />
</g>
</svg>
<script type="text/javascript">
d3.select("svg")
.append("g")
.attr("class", "dog")
.attr("transform", "translate(200 200) scale(0.5)")
.append("use")
.attr("xlink:href", "animals.svg#dog")
.on("load", function() {
console.log("And this message is not showed in firefox too.");
});
</script>
</body>
</html>
最后,我在 w3c 文档中查找,符号中的事件加载是标准的:https://www.w3.org/TR/SVG/struct.html#UseElement
有人对此有解决方法吗?
解决这个问题的很长的方法是自己处理外部资源的加载(或者更确切地说,在 d3 的帮助下)。
D3 有一个 xml 加载程序函数 (https://github.com/mbostock/d3/wiki/Requests#d3_xml),svg 是 xml,因此您可以执行以下操作:
- 准备不使用外部资源的内容
- 使用 D3 加载 SVG
- 在 D3.xml 的回调函数中,你得到一个 DOML 元素:将其插入图形中的相关位置,并执行在 "onload" 事件中要做的任何工作。
(可能有一个更简单的方法,我只是不知道)
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- 为什么这在IE中的工作方式与在Firefox中不同
- JS可以在Chrome中工作,但不能在Firefox中工作
- createElement("a") - FireFox JavaScript
- 为什么javascript:void(0)在Firefox中不起作用
- Facebook登录按钮没有'不能在Firefox上工作
- jpm的默认Firefox路径没有'不起作用
- 重载JS'firefox中的对象原型
- Ajax调用在Firefox中不会自动响应
- JS在firefox中无法正常工作
- firefox插件和dev/panel之间的通信
- IE/Chrome中未定义的函数,但Firefox中没有
- ascii输入键通过firefox中的javascript返回0
- 在firefox和chrome中的左侧显示iframe滚动条
- html5 drawImage适用于firefox,而不是chrome
- angularjs$valid-on-dates在firefox中报告错误
- 您可以使用JavaScript和Firefox扩展来更改现有页面吗
- 设置 CSS 背景在 Firefox 中不起作用
- 如何从Javascript打开Firefox
- SVG 符号不会触发加载事件(仅在 Firefox 中).我寻找解决方法