用于for循环的函数用于删除类

时间:2015-10-08 10:20:26

标签: jquery

我有以下内容:

HB.formButtons.deactivatingButton = function(element) {
    element.parent().removeClass(HB.formButtons.SUBMIT_BUTTON_STYLE);
}

并且每次我需要它时都会按照以下方式调用它:

HB.formButtons.deactivatingButton($(HB.personalDetails.SUBMIT_CHANGE_PASSWORD_SELECTOR));
HB.formButtons.deactivatingButton($(HB.personalDetails.CANCEL_CHANGE_PASSWORD_SELECTOR));

有没有办法调用一次(传递尽可能多的参数)并可能在HB.formButtons.deactivatingButton中引入一个循环?

1 个答案:

答案 0 :(得分:2)

这绝对可能如下:

HB.formButtons.deactivatingButton = function(elementsArray) {

    if($.isArray(elementsArray))
    {
        $.each( elementsArray, function( index, element){
            element.parent().removeClass(HB.formButtons.SUBMIT_BUTTON_STYLE);
        });
    }
}

只调用此函数一次,如下所示:

    var elementsArray = [$(HB.personalDetails.SUBMIT_CHANGE_PASSWORD_SELECTOR),
$(HB.personalDetails.CANCEL_CHANGE_PASSWORD_SELECTOR)];

    //call the function
    HB.formButtons.deactivatingButton(elementsArray);