2012-04-29 40 views
2

我想我可以用這個簡單的方法來創建jQuery的一個低級別的垃圾郵件過濾器 -更換鏈接屬性,以防止垃圾郵件

<a class="filter" href="mailto:johndoe[at]nowhere[dot]com">johndoe[at]nowhere[dot]com</a> 

$('.filter').each(function() { 
    $(this).html().replace(('[dot]', '.')); 
    $(this).html().replace(('[at]', '@')); 
}); 

但沒有任何反應。本地替換函數似乎無法應對jQuery。我也嘗試使用val()和text()獲取內容。也許這完全是一個不正確的方法,如果是我會欣賞一些方向。

+1

(offtopic)我用我的一個好的技巧,我創建了2個圖像:在相同的字體點(*。*)和* @ *我在頁面中使用,只是替換我需要的字符。爲什麼? :垃圾郵件可以防止* [dot] *和* [at] *。雖然'staffan estberg web com'看起來好多了**並仍然安全**。附:使用** php ** –

+0

@ RokoC.Buljan有趣的想法!在以後創建更安全的東西時,我一定會考慮到這一點。 –

回答

2

使用以下命令:

$('.filter').each(function() { 
    var that = $(this); 

    that.attr('href', that.attr('href').replace('[dot]', '.') 
            .replace('[at]', '@')); 

    that.html(that.html().replace('[dot]', '.').replace('[at]', '@')); 
}); 
+0

整潔!謝謝!只是希望我能理解爲什麼這樣做是必要的。需要學習更多的原生JS ... –

+0

字符串是***不可變的***。 'replace'函數返回一個* new *字符串。因此,需要以適當的方式使用它(捕獲結果)。 – xandercoded

+0

不知道這一點。謝謝! –

1

替換功能不會修改原始字符串。 您需要使用這樣的:

$(this).html($(this).html().replace('[dot]', '.')) 
0
$('.filter').each(function() { 
    var mail = $(this).html().replace('[dot]', '.').replace('[at]', '@'); 
    $(this).html(mail); 
    $(this).attr('href',"mailto:"+mail); 
});