Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a SVG map in my html with the <svg> tag. and I want to attach events so I can click them and trigger some events. I know I can attach click event using jQuery on polygon elements. But some areas in this map are made using paths and I'd like to trigger some events when I click inside the paths, not on the paths.

What's the way to do that? Using jQuery is preferred.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
275 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you fill a <path> then clicking inside it (on the fill) will trigger the event handler:

Demo: http://jsfiddle.net/TmsrP/1/

<path id="sauce" fill="#f00" … />    
$('#sauce').on('click',function(){ … });

You can choose to explicitly fill the path with the color transparent and mouse events will still be caught:

Demo: http://jsfiddle.net/TmsrP/2/

<path fill="transparent" … />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...