2017-02-13 15 views
0

這裏是我的屬性數組:
如果選擇具有ATTRS的陣列的ATTR

var newIDs = []; 
    $element.parents(".comments").find(".comment.new").each(function(idx, el){ 
    newIDs.push($(this).attr('commentID')); 
}); 

現在我有一個選擇:

html = $(html).find('.comment') 

我想縮小選擇來測試.comment在上面的數組中有任何到達數。

回答

1

您可以使用過濾器:

html = $(html).find('.comment').filter(function() { 
    return newIDs.indexOf($(this).attr('commentID')) > -1 
}) 

文檔here

.filter方法篩選您的jQuery收集到一組基於評價函數項。如果您的函數返回true,則該項保留,否則將被過濾掉。

0

您可以使用.filter(),迭代.comment.attributes元素

html = $(html).find('.comment').filter(function(i, el) { 
    for (var i = 0; i < el.attributes; i++) { 
    if (newIDS.indexOf(el.attributes[i].value) !== -1) { 
     return true 
    } 
    } 
    return false 
})