jQuery SVG, why can't I addClass?

前端 未结 15 2188
失恋的感觉
失恋的感觉 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:39

    Based on above answers I created the following API

    /*
     * .addClassSVG(className)
     * Adds the specified class(es) to each of the set of matched SVG elements.
     */
    $.fn.addClassSVG = function(className){
        $(this).attr('class', function(index, existingClassNames) {
            return ((existingClassNames !== undefined) ? (existingClassNames + ' ') : '') + className;
        });
        return this;
    };
    
    /*
     * .removeClassSVG(className)
     * Removes the specified class to each of the set of matched SVG elements.
     */
    $.fn.removeClassSVG = function(className){
        $(this).attr('class', function(index, existingClassNames) {
            var re = new RegExp('\\b' + className + '\\b', 'g');
            return existingClassNames.replace(re, '');
        });
        return this;
    };
    

提交回复
热议问题