0

我有一个需要遍历以返回特定长度的 div 列表。我必须将所有具有活动图像的 div 总数加起来。非活动图像由 表示alt= "missing"。我需要这个特定的长度大小来进行 ajax 交互。

问题

我怎样才能获得父 div 的长度,没有子元素的 alt 标签值缺失? (抱歉选择器中的冗长)



HTML

 <div class="project-img-container">
        <div class="modal-image-0">
            <img alt="Florence" class="featured" src="/system/works/avatars/000/000/034/medium/florence.jpg?1374131286">
        </div>
        <div class="modal-image-1">
            <img alt="Nexus" class="featured" src="/system/works/avatar_bs/000/000/034/medium/nexus.jpg?1374131286">
        </div>
        <div class="modal-image-2">
            <img alt="Missing" class="featured" src="/images/medium/missing.png">
        </div>
        <div class="modal-image-3">
            <img alt="Missing" class="featured" src="/images/medium/missing.png">
        </div>
    </div>

jQuery 成功:postImgModal

postImgModal = function(data, status) {
  var activeChildren, children, imgVal;
  imgVal = [];

  children = $(data).find('.project-img-container').children();

  /*
     children will return an object list of all divs (which in this case = 4) now I must remove the parent tags that have children img tags that have alt tag's value = "missing." 

     Lets call this var activeChildren

  */

  $.each(activeChildren, function(i, child) {
    imgVal[i] = child;
    console.log(imgVal[i]);
    return imgVal[i];
  });

  /*this loop should return a length of 2.  Opposed to before which was 4. This is because there were 2 missing alt tags above in this example html.*/

}

var activeChildren = 2; 的最终输出长度应为 2;并且 imgVal 应该只返回这两个 div

<div class="modal-image-0">
    <img alt="Florence" class="featured" src="/system/works/avatars/000/000/034/medium/florence.jpg?1374131286">
</div>
<div class="modal-image-1">
    <img alt="Nexus" class="featured" src="/system/works/avatar_bs/000/000/034/medium/nexus.jpg?1374131286">
</div>
4

4 回答 4

2
$(data).find('.project-img-container').children().filter(function() {
    return !$(this).find('[alt="Missing"]').length;
});

小提琴

要不就:

$(data).find('.project-img-container').children(':has([alt!="Missing"])');
于 2013-07-19T01:15:12.657 回答
1

我相信这应该有效

$('div > img:not([alt="Missing"])').length;

或者这个寻找特色图片。

$('div > img.featured:not([alt="Missing"])').length;
于 2013-07-19T01:15:39.013 回答
0

jQuery 属性值区分大小写,因此您必须确保它的大小写正确:

$(".project-img-container").find("img:not([alt='Missing'])")
于 2013-07-19T01:15:39.540 回答
0

您可以只使用 jQuery 选择器来满足您的需求。下面的表达式应该服务于目的。

$('div.project-img-container').find('img[alt!="Missing"]').parent();
于 2013-07-19T01:24:53.410 回答