【问题标题】:Replacing link attributes to prevent spam替换链接属性以防止垃圾邮件
【发布时间】:2012-05-09 15:31:29
【问题描述】:

我想我可以使用这种简单的方法在 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() 获取内容。也许这完全是一种不正确的方法,如果是的话,我会很感激一些方向。

【问题讨论】:

  • (offtopic) 我使用了一个不错的技巧,我创建了 2 个图像:一个点 (.) 和一个 @ 字体相同我在页面中使用,只是替换我需要的字符。为什么? : 垃圾邮件对 [dot][at] 进行了防弹。虽然staffan&lt;img src="imgz/point.png" /&gt;estberg&lt;img src="imgz/monkeey.png" /&gt;web&lt;img src="imgz/point.png" /&gt;com 看起来好多了并且仍然安全。附言使用 php
  • @RokoC.Buljan 有趣的想法!在以后创建更安全的东西时,我一定会考虑到这一点。

标签: jquery spam-prevention


【解决方案1】:

使用以下内容:

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

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

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

【讨论】:

  • 整洁!谢谢!只是希望我能理解为什么这样做是必要的。需要学习更多原生JS...
  • 字符串是不可变的replace 函数返回一个 new 字符串。因此,它需要以正确的方式使用(捕获结果)。
  • 不知道这一点。谢谢!
【解决方案2】:

替换函数不会修改原始字符串。 你需要像这样使用它:

$(this).html( $(this).html().replace('[dot]', '.') )

【讨论】:

    【解决方案3】:
    $('.filter').each(function() {
      var mail = $(this).html().replace('[dot]', '.').replace('[at]', '@');
      $(this).html(mail);
      $(this).attr('href',"mailto:"+mail);
    });
    

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      相关资源
      最近更新 更多