简体   繁体   English

有没有办法让SVG对象可点击?

[英]Is there any way to make an SVG object clickable?

I'm working with the SVG that can be viewed here: http://n1t2.info/ 我正在使用可以在这里查看的SVG: http//n1t2.info/

I would just post the SVG, but because of the street maps layer the file is extremely large. 我只想发布SVG,但由于街道地图层,文件非常大。 However, you can see if you click the link that it's a map divided into 18 different svg paths, with a number of different colors shading them. 但是,您可以看到是否单击了链接,它将地图划分为18个不同的svg路径,并使用多种不同的颜色对其进行着色。 My goal with this map is to make each of the 18 different sections clickable, or be able to give them an <a href="example.com"> . 我对此地图的目标是使18个不同部分中的每个部分都可以点击,或者能够为他们提供<a href="example.com"> Is this possible with SVGs at all? 这有可能与SVG一起使用吗?

edit: A comment suggested a show the way that I'm constructing the SVG file. 编辑:评论建议显示我正在构建SVG文件的方式。 You can see my method for that here. 你可以在这看到我的方法

You can handle it pretty easily by adding a data attribute to each <path> element and then handle it via jquery or just javascript. 通过向每个<path>元素添加data属性,然后通过jquery或javascript处理它,您可以非常轻松地处理它。

  1. First, add something like data-url="http://your-url.com" to the <path> element. 首先,在<path>元素中添加类似data-url="http://your-url.com"的内容。 Example: <path data-url="http://your-url.com"> . 示例: <path data-url="http://your-url.com">

  2. Add jquery library just before the closing of your </body> tag, and the script in step #3 just like: 在关闭</body>标记之前添加jquery库,步骤#3中的脚本就像:

      <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <script> //your script </script> </body> </html> 
  3. instead of //your script you'll paste this one: 而不是//your script你将粘贴这个:

     $(document).ready(function(){ $('path').on('click', function(e){ e.preventDefault(); var url = $(this).data('url'); console.log('Moving to:', url); window.open(url, '_blank'); }); }); 
  4. Test it: http://jsfiddle.net/jpvu852d/ 测试一下: http//jsfiddle.net/jpvu852d/

IMO, the best solution is the SVG <a> element. IMO,最好的解决方案是SVG <a>元素。

 <svg width="200" height="200" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <a xlink:href="http://stackoverflow.com/questions/15532371/do-svg-docs-support-custom-data-attributes"> <path d="M 100 100 L 300 100 L 200 300 z" fill="orange" stroke="black" stroke-width="3"></path> </a> </svg> 

A search on google turned up this jQuery plugin for decorating image maps that may help you achieve your goal: 谷歌搜索出现了这个jQuery插件,用于装饰图像地图,可以帮助您实现目标:

http://www.outsharked.com/imagemapster/default.aspx?what.html http://www.outsharked.com/imagemapster/default.aspx?what.html

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

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