jQuery SVG, why can't I addClass?

前端 未结 15 2085
失恋的感觉
失恋的感觉 2020-11-22 02:25

I am using jQuery SVG. I can\'t add or remove a class to an object. Anyone know my mistake?

The SVG:



        
15条回答
  •  野性不改
    2020-11-22 02:49

    I wrote this in my project, and it works... probably;)

    $.fn.addSvgClass = function(className) {
    
        var attr
        this.each(function() {
            attr = $(this).attr('class')
            if(attr.indexOf(className) < 0) {
                $(this).attr('class', attr+' '+className+ ' ')
            }
        })
    };    
    
    $.fn.removeSvgClass = function(className) {
    
        var attr
        this.each(function() {
            attr = $(this).attr('class')
            attr = attr.replace(className , ' ')
            $(this).attr('class' , attr)
        })
    };    
    

    examples

    $('path').addSvgClass('fillWithOrange')
    $('path').removeSvgClass('fillWithOrange')
    

提交回复
热议问题