1

我正在尝试开发交互式 SVG 地图,当鼠标进入 SVG 图像中的矩形时,我想做一些事情。目前,当我的鼠标进入 SVG 图像时,代码会记录到控制台,但当我将鼠标悬停在矩形上时不会。任何帮助将非常感激。谢谢!

<object onload="svgOnLoad()" id="activeSVG" data="SVGNAME.svg" type="image/svg+xml">
</object>

  <script>
            function svgOnLoad() {
              console.log("SVG Loaded");
              $("#activeSVG").mouseenter(function(e) {
                console.log("In the SVG")
              });

              //Executed when the mouse enters an SVG rect element
              $("rect").mouseenter(function(e) {
                console.log("Mouse Entered rectangles!!")
              });
            }
  </script>

4

2 回答 2

0

在弄乱了一点之后,我发现了需要添加的内容。您需要获取 svg 对象的内容,然后从那里使用它。我在下面包含了新的脚本代码。

<script>
            function svgOnLoad() {
              // Get SVG object
              var officeMapFloor = document.getElementById("activeSVG");

              // Get the content of the SVG object
              var svgDoc = officeMapFloor.contentDocument;

              // Access an ID within the content of the object
              var rect = svgDoc.getElementById("siesta");

              // Apply the event listener
              rect.onmouseover = function(){
                console.log("Finally in the rectangle");
              };

            }
 </script>

于 2017-06-05T18:42:06.440 回答
0

有一个简短的描述,例如,在https://www.tutorialspoint.com/svg/svg_interactivity.htm上。

对于mouseover您需要该jQuery mouseover功能的事件:https ://api.jquery.com/mouseover/

于 2017-06-05T17:54:23.637 回答