我有一个 div 列表,需要遍历它才能返回特定长度。我必须将所有具有事件图像的 div 总数加起来。非事件图像由 alt=“missing”
表示。我需要这个特定的长度大小来进行 ajax 交互。
问题
如何获取没有 alt 标签值缺失的子元素的父 div 的长度? (对于选择器中的冗长感到抱歉)
<小时/> <小时/>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>
最佳答案
$(data).find('.project-img-container').children().filter(function() {
return !$(this).find('[alt="Missing"]').length;
});
或者只是:
$(data).find('.project-img-container').children(':has([alt!="Missing"])');
关于javascript - 返回 div 列表的长度,但其子级没有特定属性值除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17736562/