Svg click events not working properly

2019-06-26 02:37发布

I am trying to make my SVGs look like a "pie shape" which appears to be all fine, besides, i want each of them to have a different click event.

function one() {
    alert("1");
}
function two() {
    alert("2");
}
function three() {
    alert("3");
}
function four() {
    alert("4");
}
<svg style="position: absolute;height:auto;width:auto;" onClick="one()">
<path d="m 25.857864,25.857865 a 20,20 0 0 1 28.284271,-1e-6 L 40,40 Z"/>
</svg>
<svg style="position: absolute;height:auto;width:auto;" onClick="two()">
<path
   d="m 25.857864,-54.142135 a 20,20 0 0 1 28.284271,-10e-7 L 40,-40 Z" 
   transform="rotate(90)" />
</svg>
<svg style="position: absolute;height:auto;width:auto;" onClick="three()">
<path
   d="m -54.142136,-54.142135 a 20,20 0 0 1 28.284271,-10e-7 L -40,-40 Z" 
   transform="scale(-1)" />
</svg>
<svg style="position: absolute;height:auto;width:auto;" onClick="four()">
<path 
   d="m 25.857864,25.857865 a 20,20 0 0 1 28.284271,-1e-6 L 40,40 Z" 
   transform="matrix(0,1,1,0,0,0)"/>
</svg>

However my problem is, whenever i try, the last SVG in the code seems to be covering other SVGs in the code, such that only the last function {four() in the example} would be called out no matter which part of the circle i click on

1条回答
▲ chillily
2楼-- · 2019-06-26 02:59

One svg tag and assign onClick function to path tag like this and it works fine:

function one() {
    alert("1");
}
function two() {
    alert("2");
}
function three() {
    alert("3");
}
function four() {
    alert("4");
}
<svg style="position: absolute;height:auto;width:auto;">
<path d="m 25.857864,25.857865 a 20,20 0 0 1 28.284271,-1e-6 L 40,40 Z" onClick="one()"/>
<path
   d="m 25.857864,-54.142135 a 20,20 0 0 1 28.284271,-10e-7 L 40,-40 Z" 
   transform="rotate(90)" onClick="two()"/>
<path
   d="m -54.142136,-54.142135 a 20,20 0 0 1 28.284271,-10e-7 L -40,-40 Z" 
   transform="scale(-1)" onClick="three()" />

<path 
   d="m 25.857864,25.857865 a 20,20 0 0 1 28.284271,-1e-6 L 40,40 Z" 
   transform="matrix(0,1,1,0,0,0)" onClick="four()"/>
</svg>

查看更多
登录 后发表回答