最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MS Edge can't detect delegated events for <use> SVG elements? - Stack Overflow

programmeradmin3浏览0评论

I think I've found a troubling bug with MS Edge that impacts on dynamically created SVG <use> elements. Edge seems to be able to detect directly bound events, i.e. $('.use').on('click', ...), however delegated events $('body').on('click', 'use', ...) are ignored.

It is most easily illustrated with a JS Fiddle (tested in Chrome, where both bindings in work and in Edge where the delegated binding doesn't work):
/

Does anyone have any insight on this issue, and knows of a possible workaround? Foremost, I'm looking for a solution where we can still use the <use> elements as it's imperative for our SPA.

I think I've found a troubling bug with MS Edge that impacts on dynamically created SVG <use> elements. Edge seems to be able to detect directly bound events, i.e. $('.use').on('click', ...), however delegated events $('body').on('click', 'use', ...) are ignored.

It is most easily illustrated with a JS Fiddle (tested in Chrome, where both bindings in work and in Edge where the delegated binding doesn't work):
https://jsfiddle/Lr0arahb/

Does anyone have any insight on this issue, and knows of a possible workaround? Foremost, I'm looking for a solution where we can still use the <use> elements as it's imperative for our SPA.

Share Improve this question edited Feb 7, 2017 at 0:30 Web_Designer 74.8k93 gold badges210 silver badges267 bronze badges asked Jan 27, 2016 at 15:48 Anton AbilovAnton Abilov 3433 silver badges11 bronze badges 2
  • 1 You do realize that IE11 and Microsoft Edge are two pletely separate browsers, right? – krillgar Commented Jan 27, 2016 at 15:55
  • Haha, I did not, that's quite alarming, the question has been edited, this is for Edge. Thanks for clearing that up! – Anton Abilov Commented Jan 27, 2016 at 16:02
Add a ment  | 

2 Answers 2

Reset to default 7

I too have found this with Edge 13. It's definetly a hack rather than a solid solution but to work around it I put the SVG in a container and used a transparent pseudo-element to cover the SVG. This way the pseudo-element gets the click rather than the SVG.

Example SVG icon used in a button:

<button class="close" type="button">
  <svg role="img" class="icon icon--close">
    <use xlink:href="icons.svg#close" />
  </svg>
</button>

CSS Fix:

.close {
  /* Make it so the pseudo-element is relative to this parent element */
  position: relative;
}

.close:after {
  /* Cover the button with a pseudo-element so the SVG can't be clicked */
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

You can use :before or :after pseudo-elements.

Using pointer-events: none can solve this. Instead of applying it to the <svg> tag and attaching handlers to a container element as is mentioned in a ment, if you add pointer-events: none to the <use> itself, this prevents it from stopping event propagation, and the outer <svg> can still be receptive to mouse events.

发布评论

评论列表(0)

  1. 暂无评论