This my be a silly question but can I have an If within a for in Jquery?

The code is

if (this.className == 'label_radio r_off' || inp.checked) {
    var ls = gebtn(this.parentNode,'label');
    for (var i = 0; i < ls.length; i++) {
        var l = ls[i];
        console.log(l);
        if (l.className.indexOf('label_radio') == -1)  continue;
        l.className = 'label_radio r_off';
    };
    this.className = 'label_radio r_on';
    if (safari) inp.click();
} else {
    this.className = 'label_radio r_off';
    if (safari) inp.click();
};

Im trying to add a if within the first

for (var i = 0; i < ls.length; i++) {
    var l = ls[i];
    console.log(l);
    if (l.className.indexOf('label_radio') == -1)  continue;
    l.className = 'label_radio r_off';
};

I dont want to run this if the label has a class of "disabled " I was think something like this but cant get it working

for (var i = 0; i < ls.length; i++) {
    var l = ls[i];
    console.log(l);
    if (l.className == 'disabled') {
        alert('hello');
    } else {
        if (l.className.indexOf('label_radio') == -1)  continue;
        l.className = 'label_radio r_off';
    }; 
};
有帮助吗?

解决方案

If you are already using jQuery, use its functions, like hasClass

The .hasClass() method will return true if the class is assigned to an element, even if other classes also are.

Source: http://api.jquery.com/hasClass/

Resulting in something like:

for (var i = 0; i < ls.length; i++) {
    var l = ls[i];
    console.log(l);
    if ($(l).hasClass('disabled')) {
        alert('hello');
    } else {
        if ($(l).hasClass('label_radio'))  continue;
        l.className = 'label_radio r_off';
    }; 
};

其他提示

Thank for all the help.... and comments

should have been

if (l.className == 'disabled label_radio') {

     } else {
           if (l.className.indexOf('label_radio') == -1)  continue;
            l.className = 'label_radio r_off';
     }; 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top