jQuery .hasClass() method fails for SVG elements

后端 未结 5 1850
挽巷
挽巷 2021-02-18 14:51

I have a set of SVG elements with the classes node and link. My program should detect whether an element has the node class or the l

5条回答
  •  旧时难觅i
    2021-02-18 15:16

    As Bergi pointed out in comments, jQuery silently fails on SVG elements on account of className returning an SVGAnimatedString object instead of a normal DOMString.

    See this JSFiddle for a comparison.

    I was tempted to submit a pull request on this, but did a quick project search, and apparently the jQuery project stance on SVG issues is wontfix: https://github.com/jquery/jquery/pull/1511

    If you're using D3, you could use d3.select(this).classed('node'). Note that D3 correctly returns for both HTML elements and SVG elements.

提交回复
热议问题