2013-02-18 80 views
0

我想過濾器值類似,我試着按照以下js代碼,但此代碼無法過濾值111因爲這個沒有,。我的HTML代碼如下(有些做,有些不這樣做:,)。過濾器值類似

如何解決js代碼過濾所有字符串?

DEMO:http://jsfiddle.net/bNuRE/

HTML:

<div class="oo">111</div> 
<div class="oo">111, 222, 333</div> 
<div class="oo">444, 111</div> 
<div class="oo">222, 333</div> 
/////////////////////////////////////////////////////// 
<div class="ww">111</div> 
<div class="ww">777</div> 
<div class="ww">333</div> 
<div class="ww">666</div> 

JS:

var valeache = $('.oo').text().split(', '); 
$.each(valeache, function (a, val) { 
    $('.ww').filter(function() { 
     return $(this).text() == val 
    }).remove(); 
}); 
+0

我不明白是什麼問題。你的演示似乎工作正常。 – bfavaretto 2013-02-18 18:32:10

+0

ok,'.oo'中的'111'和'.ww'中的'111'類似,但是對此不起作用。因爲在'.oo'的'111'中沒有''''。 – 2013-02-18 18:33:36

+0

@TaylorGomez,'.oo'中的'333'是否也被過濾了? – Alexander 2013-02-18 18:45:02

回答

1

你的valeache建設是錯誤的。

您需要迭代每個.oo元素才能正確構造valeache

var valeache = $('.oo').map(function(){ 
    return $(this).text().split(', '); 
}).get(); 
$.each(valeache, function (a, val) { 
    $('.ww').filter(function() { 
     return $(this).text().trim() == val; 
    }).remove(); 
}); 
+0

嗯,我仍然看到奇怪的東西,給我一分鐘 – Alexander 2013-02-18 18:41:49

+0

這創建了一個數組。 – undefined 2013-02-18 18:41:51

+0

@undefined,你確定嗎?請注意,我所描述的「奇怪」的事情是,沒有證據表明「333」應該過濾或不是我害怕。我會等待OP的回覆:) – Alexander 2013-02-18 18:46:25

0
var valeache = null; 

$('.oo').text(function(i, text) { 
    valeache += text.replace(/\s/g,'') + ','; 
}) 
valeache = $.unique(valeache .replace(/,$/,'').split(',')); 

$.each(valeache, function (a, val) { 
    $('.ww').filter(function() { 
     return $(this).text() == val 
    }).remove(); 
}); 

演示:http://jsfiddle.net/xfSK4/3/

0

這並不完全清楚,我你想要做什麼。你是否試圖從目標列表中刪除其值與來自源列表的任何元素(可能是逗號分隔)的值之一匹配的元素?如果是這樣,我覺得你可以更容易地做到這一點:

var vals = $.map($('.oo'), $.text).join(", ").split(", "); 

$('.ww').filter(function() { 
    return $.inArray($(this).text(), vals) > -1; 
}).remove(); 

演示:http://jsfiddle.net/CrossEye/MVGLQ/1/

那最初的數組包含重複,但不應該是一個問題。