jQuery .hasClass() method fails for SVG elements

后端 未结 5 1815
挽巷
挽巷 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条回答
  •  既然无缘
    2021-02-18 15:29

    The class attribute for HTML element doesn't have the same meaning in SVG.

    $("").addClass($(this).attr("class")).hasClass("node")
    

    Or

    /(^|\s)node(\s|$)/.test($(this).attr("class"))
    

    for SVG elements.

    EDIT .hasClass seems to work just fine (at least in IE9 and FF) http://jsfiddle.net/X6BPX/1/

    So the problem could be any combination of the following: a syntax error, using an outdated browser, using an outdated version of jQuery.

提交回复
热议问题