javascript - What does it mean when newClassName = element.className changes its position?
高洛峰
高洛峰 2017-05-19 10:23:59
0
7
611

Correct function:

function addClass(element,value) {
    if(!element.className) {
        element.className = value;
    } else {
        newClassName = element.className;
        newClassName += " ";
        newClassName += value;
        element.className = newClassName;
    }
}

Is it okay to write it like this?

function addClass(element,value) {
    if(!element.className) {
        element.className = value;
    } else {
        
        newClassName += " ";
        newClassName += value;
        
}
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(7)
过去多啦不再A梦

element.classList.add() is more convenient


If you want to be compatible with IE10 or below, then I have nothing to say

曾经蜡笔没有小新

is equivalent to

element.className = element.className + " " + value;
洪涛

There is a compatibility issue

element.classList.add('xxx');

Equivalent to the above, solving compatibility issues

element.className+=''+'xxx';

Other packaging methods

function hasClass(elements, cName) {
    return !!elements.className.match(new RegExp('(\s|^)' + cName + '(\s|$)'));
};

function addClass(elements, cName) {
    if (!hasClass(elements, cName)) {
        elements.className += ' ' + cName;
    };
};

function removeClass(elements, cName) {
    if (hasClass(elements, cName)) {
        elements.className = elements.className.replace(new RegExp('(\s|^)' + cName + '(\s|$)'), ' ');
    };
};
阿神

The answer is definitely not possible

function addClass(element, value) {
    if (!element.className) {
        element.className = value;
    } else {
        newClassName += " ";
        newClassName += value;

    }
}

What this needs to be implemented is to add a class to the dom element. elseIn the branch, you only operate a string. What is the use if it is not set to the dom element in the end? For the added class to take effect, it must be placed on the dom element

In addition, there is also a problem with your correct writing:

function addClass(element,value) {
    if(!element.className) {
        element.className = value;
    } else {
        // newClassName未声明 ,隐式全局变量
        newClassName = element.className;
        newClassName += " ";
        newClassName += value;
        element.className = newClassName;
    }
}

In factelsebranching can be simplified

function addClass(element, value) {
    if (!element.className) {
        element.className = value;
    } else {
        element.className += ' ' + value;
        // += 运算符优先级较低,实际相当于
        // element.className += (' ' + value);
        // 即
        // element.className = element.className + (' ' + value);
    }
}
習慣沉默

Can’t. The purpose of the function is to give element加上一个叫value的类,你else里面根本没有对element.className进行赋值怎么行,那个newClassName只是个变量,用来保存现有class,并加上value这个新class后,塞回element.className, so

First:

newClassName = element.className;//记下现有

is followed by:

element.className = newClassName;//塞回元素

In fact, the original function has a better way of writing. elseThere is no need to get a variable in it, just spell it directly:

else {
element.className += " " + value;
}
我想大声告诉你

No, it’s not possible. NewClass must be assigned first after the else statement before the operation can be reassigned to element.className. If it is removed, it seems that there is no such function

Peter_Zhu

I just saw it wrong...

No way

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template