在SVG文件调用Javascript脚本有三种方式
1、在SVG内部嵌入脚本,如下:
<svg width="400" height="200">
<script><![CDATA[
function showmsg()
{
alert("you had clicked the green rect");
}
]]></script>
<g id="rect1">
<rect id="rectangle" onclick="showmsg()" x="50" y="50" width="100" height="50" style="fill:green"/></g>
</svg>
showmsg函数嵌入在svg文件内部。
2、导入.js文件
<script xlink:href="test.js"></script>
3、在执行html中的脚本
分两种情况,在IE6中直接调用脚本例如:
在html中的函数
<script type="text/javascript">
function test(){
}
</script>
在svg中可以直接调用,如onclick="test()"
但是在IE9或者opera 11.5中确提示test()未定义,必须使用top.test()调用才可以。