[英]event.target in SVG padding?
I have an SVG
that 'm using as a drawing surface. 我有一个用作绘图表面的SVG
。 It contains all sorts of sub-elements, and in this case, the one I care about is a circle
. 它包含各种子元素,在这种情况下,我关心的是一个circle
。 In an effort to be able to draw all the way to the edge of the svg
, I've added padding: 5px
to the svg
element, which works as intended, with one small wrinkle. 为了能够一直绘制到svg
的边缘,我在svg
元素上添加了padding: 5px
,它可以按预期工作,并且有一点皱纹。
Click events on a circle
in the svg
element itself return event.target.nodeName == circle
. svg
元素本身中的circle
上的单击事件返回event.target.nodeName == circle
。 But click events on a circle
in the padding returns event.target.nodeName == svg
, which effectively makes the circle
unselectable (since I can't click it). 但是在填充中的circle
上单击事件会返回event.target.nodeName == svg
,这实际上使该circle
不可选择(因为我无法单击它)。 Is there a way to see the actual event.target
within an element's padding like this? 有没有办法像这样在元素的填充内查看实际的event.target
? Or do I need to build a workaround (ie mapping the coordinates of the circles I've placed, and checking the svg
's event.currentTarget
against those coordinates each time)? 还是我需要建立一个解决方法(即映射我放置的圆的坐标,并每次对照这些坐标检查svg
的event.currentTarget
)?
Updated: Here's a fiddle illustrating the issue. 更新:这是一个小提琴,说明了这个问题。 Click the left circle (inside the padding) and event.target.nodeName == svg
; 单击左侧圆圈(在填充内)和event.target.nodeName == svg
; click the right circle (in the svg proper) and event.target.nodeName == circle
. 点击右侧的圆圈(在svg中为正确),然后点击event.target.nodeName == circle
。
https://jsfiddle.net/r5jd87n6/2/ https://jsfiddle.net/r5jd87n6/2/
Seems to work OK at least on Firefox once I add overflow: visible to the outer <svg>
element. 添加溢出后,似乎至少在Firefox上可以正常工作:对外部<svg>
元素可见。
$('#parentsvg').click(function(event){ $('#output').html("event.target.nodeName: " + event.target.nodeName); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <svg id="parentsvg" width="200" height="200" style="padding:15px; overflow:visible" > <circle id="clickableCircle" cx="100" cy="50" r="6" fill="#333333"></circle> <circle id="unClickableCircle" cx="-5" cy="50" r="6" fill="#333333"></circle> </svg> </div> <div id="output"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.