Jquery Hasclass在each()中的用法

Jquery Hasclass在each()中的用法,jquery,Jquery,这是我的脚本,如果div带有class.field collection container,我想添加类,但我想将其应用于div,该类不包含。field name field basic info,请阅读html $('.field-collection-container').each(function (index, obj) { if (($(this).hasClass('field-name-field-basic-info')) == false) { $(t

这是我的脚本,如果div带有class
.field collection container
,我想添加类,但我想将其应用于div,该类不包含
。field name field basic info
,请阅读html

$('.field-collection-container').each(function (index, obj) {
    if (($(this).hasClass('field-name-field-basic-info')) == false) {
        $(this).find('.field-items .content .field-item').each(function (index, obj) {
            //alert($(this).text());
            if ($(this).text() == '1') {
                $(this).addClass("tick1");
            } else {
                $(this).addClass("tick0");
            }
        });
    }
});
html:

<div class="field-collection-container field-name-field-basic-info">not apply</div> 
<div class="field-collection-container">apply</div>
不适用
应用

我使用hasClass尝试这样做,但没有成功。

您的代码应该可以工作。但是,您可以使用
not
方法排除元素,使用
addClass
方法添加类名,而不是创建许多不必要的jQuery对象

$('.field-collection-container')
     .not('.field-name-field-basic-info')
     .find('.field-item').addClass(function() {
           return $(this).text() === '1' ? 'tick1' : 'tick0';
     });

我喜欢Reigel的回答因为他用了op的密码。可惜它不见了