8
我创建了一个小页面,就像这一个在这里:内SVG调用JavaScript函数从周围的HTML
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function test() {
executeAnimation();
}
</script>
</head>
<body>
<h1>Tester</h1>
<embed src="test.svg" type="image/svg+xml" />
<hr />
<input type='button' value='Test' onclick='test()' />
</body>
</html>
的test.svg
看起来是这样的:
<?xml version="1.0" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg id="testSvgId" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="950" height="900">
<title>Title</title>
<desc>Desc</desc>
<defs>
<script type="text/javascript">
<![CDATA[
function executeAnimation() {
document.getElementById('anim').beginElement();
}
]]>
</script>
</defs>
<ellipse cx="500" cy="1090" rx="600" ry="0" fill="rgb(94,114,54)">
<animate id="anim" attributeType="XML" attributeName="ry" begin="indefinite" dur="2s" from="0" to="350" fill="freeze" />
</ellipse>
</svg>
正如你所看到的,我要从HTML页面中的JavaScript调用函数executeAnimation()
,该函数在SVG图像中定义。这实际上不起作用。
我也试过这样:
<svg onload='onloadFunction()'...>
...
function onloadFunction() {
alert('i am loading');
window.executeAnimation = executeAnimation();
}
这在另一个论坛上建议,但这丝毫也不起作用(window.executeAnimation
从外面未定义)。
什么才是真正正确的方法来做到这一点?
五年后的第一条评论,我只注意到我在SVG代码前面忘了一个空格,因此只显示“标题描述”。 NOBODY发布了五年! :-) SVG几天后在2012年工作,所以我想现在已经解决了:P – 2017-06-07 15:01:08
太棒了!我甚至没有看到日期!哈哈。 – dennispreston 2017-06-07 15:04:57