2012-01-17 81 views
0

我剛開始使用jQuery,並且想用它來解決這個問題:我有一個來自當前駐留在div中的不同域的電子郵件地址列表。使用.each(),要循環訪問每個電子郵件地址,只挑選那些沒有「@ gmail.com」域名的郵件地址。換句話說,[email protected]被附加到另一個div,文件[email protected]不會。我怎樣才能做到這一點?使用jQuery來過濾電子郵件地址

jQuery的

目前的代碼只是抓住所有的電子郵件,但確實沒有過濾

$('.email_address').each(function() { 
    $(this).html().appendTo('#filtered_email_address'); 
}); 

回答

2

很簡單:

$('.email_address').not(':contains("@gmail.com")').each(function() { 
    $(this).clone().appendTo('#filtered_email_address'); 
}); 

如果你特別希望進行篩選,嘗試這個:

$('.email_address').filter(function() { 
    return $(this).text().indexOf('@gmail.com') != 0; 
}).each(function() { 
    $(this).clone().appendTo('#filtered_email_address'); 
}); 
0

你可以做到這一點很容易與indexOf方法:

$('.email_address').each(function() { 
    var email = $(this).text() 
    if (email.indexOf('@gmail.com') == -1) 
     ('#filtered_email_address').append(email); 
});