jQuery SVG, why can't I addClass?

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

    Here is my rather inelegant but working code that deals with the following issues (without any dependencies):

    • classList not existing on elements in IE
    • className not representing the class attribute on elements in IE
    • Old IE's broken getAttribute() and setAttribute() implementations

    It uses classList where possible.

    Code:

    var classNameContainsClass = function(fullClassName, className) {
        return !!fullClassName &&
               new RegExp("(?:^|\\s)" + className + "(?:\\s|$)").test(fullClassName);
    };
    
    var hasClass = function(el, className) {
        if (el.nodeType !== 1) {
            return false;
        }
        if (typeof el.classList == "object") {
            return (el.nodeType == 1) && el.classList.contains(className);
        } else {
            var classNameSupported = (typeof el.className == "string");
            var elClass = classNameSupported ? el.className : el.getAttribute("class");
            return classNameContainsClass(elClass, className);
        }
    };
    
    var addClass = function(el, className) {
        if (el.nodeType !== 1) {
            return;
        }
        if (typeof el.classList == "object") {
            el.classList.add(className);
        } else {
            var classNameSupported = (typeof el.className == "string");
            var elClass = classNameSupported ?
                el.className : el.getAttribute("class");
            if (elClass) {
                if (!classNameContainsClass(elClass, className)) {
                    elClass += " " + className;
                }
            } else {
                elClass = className;
            }
            if (classNameSupported) {
                el.className = elClass;
            } else {
                el.setAttribute("class", elClass);
            }
        }
    };
    
    var removeClass = (function() {
        function replacer(matched, whiteSpaceBefore, whiteSpaceAfter) {
            return (whiteSpaceBefore && whiteSpaceAfter) ? " " : "";
        }
    
        return function(el, className) {
            if (el.nodeType !== 1) {
                return;
            }
            if (typeof el.classList == "object") {
                el.classList.remove(className);
            } else {
                var classNameSupported = (typeof el.className == "string");
                var elClass = classNameSupported ?
                    el.className : el.getAttribute("class");
                elClass = elClass.replace(new RegExp("(^|\\s)" + className + "(\\s|$)"), replacer);
                if (classNameSupported) {
                    el.className = elClass;
                } else {
                    el.setAttribute("class", elClass);
                }
            }
        }; //added semicolon here
    })();
    

    Example usage:

    var el = document.getElementById("someId");
    if (hasClass(el, "someClass")) {
        removeClass(el, "someClass");
    }
    addClass(el, "someOtherClass");
    

提交回复
热议问题