0

这是到目前为止的代码。我想访问<div class="field-item even">1</div>的值1,然后如果值为1,则添加一个类,如果值为0,则添加另一个类。我的代码只能访问第一个字段集合容器,我无法找出问题所在。

$(document).ready(function () {  
        jQuery.each($('.field-collection-container'), function (i, obj) {
            alert($(this).find('.field-items .content .field-item').text());
        })

});

html

<div class="container">
<div class="field-collection-container clearfix">
    <div class="field field-name-field-interview-feeling field-type-field-collection field-label-above">
    <div class="field-label">TEXT</div>
        <div class="field-items">
        <div class="field-item even">
            <div class="field-collection-view clearfix view-mode-full field-collection-view-final">
                <div class="entity entity-field-collection-item field-collection-item-field-interview-feeling clearfix" about="/dtesthkinterview/field-collection/field-interview-feeling/4" typeof="">
                  <div class="content">
                    <div class="field field-name-field-19 field-type-list-boolean field-label-above">
                        <div class="field-label">TEXT</div>
                        <div class="field-items">
                            <div class="field-item even">1</div>
                        </div>
                    </div>
                    <div class="field field-name-field-no-people field-type-list-boolean field-label-above">
                        <div class="field-label">TEXT</div>
                        <div class="field-items"><div class="field-item even">0</div></div>
                    </div>
                    <div class="field field-name-field-untrust field-type-list-boolean field-label-above">
                        <div class="field-label">TEXT</div>
                        <div class="field-items"><div class="field-item even">0</div></div>
                    </div>
                    <div class="field field-name-field-waste-money field-type-list-boolean field-label-above">
                        <div class="field-label">TEXT</div>
                        <div class="field-items"><div class="field-item even">0</div></div>
                    </div>  
                  </div>
                </div>
            </div>
        </div>
        </div>
    </div>
</div>
<div class="field-collection-container clearfix">
.....
</div>
<div class="field-collection-container clearfix">
.....
</div>

4

7 回答 7

2
$('.field-collection-container .field-items .field-item').addClass(function () {
    return $(this).text() === "1" ? "one-class" : "another-class";
});

请注意,您根本不需要.each()

$.trim($(this).text())如果您希望存在空格,则可以使用。

于 2013-05-06T08:58:09.070 回答
1
jQuery.each($('.field-collection-container .field-items .content .field-item'), function (i, obj) {
    if($(this).text() == "1"){
       $(this).addClass("someclass");
    }else if($(this).text() == "0"){
        $(this).addClass("anotherClass");
    }
});

工作示例 http://jsfiddle.net/P9meu/

于 2013-05-06T08:56:36.173 回答
0
 <div class="field-items">
    <div class="field-item even">
        <div class="field-collection-view clearfix view-mode-full field-collection-view-final">
            <div class="entity entity-field-collection-item field-collection-item-field-interview-feeling clearfix" about="/dtesthkinterview/field-collection/field-interview-feeling/4" typeof="">
              <div class="content">
                <div class="field field-name-field-19 field-type-list-boolean field-label-above">
                    <div class="field-label">TEXT</div>
                    <div class="field-items">
                        <div ***id="id1"*** class="field-item even">1</div>
                    </div>

而不是这样调用

  $(document).ready(function () {  
    jQuery.each($('.field-collection-container'), function (i, obj) {
        alert($(this).find('.field-items .content .field-item').text());
    })

});

你可以使用 id 作为 div 并像这样调用

    $(document).ready(function () {  
    {  
       var t=$("#id").text();
       alert(t);
       if(t==1)
          {
            $("#id1").addClass(".classname");
          }
       else
         {
         if(t==0)
          {
            $("#id1").addClass(".anotherclassname");
          }
         }

     })
于 2013-05-06T09:02:54.883 回答
0

在 jsFiddle 上检查此示例

http://jsfiddle.net/vijaypatel/Q2GHV/

$(function () {
    $('.field-collection-container').each(function (index, obj) {
        $(this).find('.field-items .content .field-item').each(function (index, obj) {
            alert($(this).text());
            if ($(this).text() == '1') {
                $(this).prop('class','One');
            } else {
                $(this).prop('class','Zero');
            }
        });
    });
});

上面的代码与多个“field-collection-container”一起工作得很好,并且还修改了奇偶元素

于 2013-05-06T09:03:13.260 回答
0

请找到以下代码,我希望它能解决您的问题

$("div.content").find("div.field-item").each(function () {
                if ($(this).html() == 1)
                    $(this).addClass("evenDivClass");
                else if ($(this).html() == 0)
                    $(this).addClass("oddDivClass");
            });
于 2013-05-06T09:03:25.197 回答
0

如果你真的只想要不同类中的交替 fielditem div,你可以尝试

$.each($(".field-item").find(".even"), function(i,o){ if(o.textContent==="1") $(o).addClass("some"); if(o.textContent==="0") $(o).addClass("other");)

这是短而快的。

于 2013-05-06T09:07:10.507 回答
0

这是一个javascript解决方案的示例

var fieldItems = document.querySelectorAll(".field-collection-container .field-item");

Array.prototype.forEach.call(fieldItems, function (fieldItem) {
    var text = fieldItem.textContent.trim();

    if (text === "0") {
        fieldItem.className += " Zero";
    } else if (text === "1") {
        fieldItem.className += " One";
    }
});

jsfiddle 上

于 2013-05-06T09:29:58.730 回答