Check if element contains any of the class from array

后端 未结 6 1035
一整个雨季
一整个雨季 2021-01-17 06:28

I have the following elements:

6条回答
  •  攒了一身酷
    2021-01-17 07:09

    $('div').each(function () {
        var found = false;
        var element_classes = $(this)[0].className.split(/\s+/);
    
        // Loop each class the element has
        for (var i = 0; i < element_classes.length; i++) {
            // Check if each class from the element is within the array of classes we want to match
            if (['nine', 'ten', 'eleven'].indexOf(element_classes[i]) !== -1) {
                // We found a match, break out of the loop
                found = true;
                break;
            }
        }
    
        // check if found or not
        if (found) {
            // Was found
        }
        else {
            // Was not found
        }
    
    });
    

提交回复
热议问题