繁体   English   中英

从svg类引用单独的Javascript文件

[英]Reference separate Javascript file from svg class

我正在尝试将javascript函数(在单独的js文件中)用于svg组件的onclick事件。 我尝试从svg节点中引用文件,但是出现错误,提示函数undefined 它确实说未声明script标记内的src属性。

我尝试将scr更改为xlink:href并将xmls:xlink属性添加到svg,但是我遇到了同样的问题。

如果我在svg节点内添加该功能,则效果很好。

如何以及在何处使用要调用的函数引用js文件?

这是我的SVG文件的内容。 我要调用的方法是另一个文件中的ShowContextMenu()。

  <svg class="SystemOverview" id="m" currentScale="1" xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 900 500">

<svg class="p21" componentType="Valve2Way" onclick="ShowContextMenu()">    
  <line x1="73" y1="40" x2="73" y2="45" stroke = "black"></line>
  <line x1="73" y1="62" x2="73" y2="66" stroke = "black"></line>
  <polygon id="p21IN" points="65,43 73,53 80,43" stroke="black" fill="white"></polygon>
  <polygon id="p21OUT" points="65,62 73,53 80,62" stroke="black" fill="white"></polygon>
  <polygon id="p21ALERT" points="60,40 85,40 85,66 60,66" stroke="none" stroke-width="2" fill="none"></polygon>
</svg>

外部javascript应放在<head>部分中。 您的其余代码看起来是正确的。

<html>
    <head>
        <script type="type="text/javascript" src="path/to/file.js"></script>
    </head>
    <body>
        ...
    </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM