使用jQuery隐藏电子邮件地址

7
我知道有一些插件可以做到这一点,但我需要的是非常具体的东西,而且我找不到任何东西。
我需要一个jQuery脚本,只更改mailto链接,而不更改链接文本。例如: <a href="person(at)exammple.com">Person's Email</a> 我需要在链接内显示实际电子邮件地址之外的其他内容。我试图使用的脚本来自这个网站
以下是脚本:
jQuery.fn.mailto = function() {
    return this.each(function(){
        var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
        $(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
    });
};

我认为我需要在<a>标签之间用其他内容替换"email"变量,但我不确定用什么。我对jQuery还是很新手。谢谢。
3个回答

8
这个怎么样:
(function($) {
    jQuery.fn.mailto = function() {
        return this.each(function() {
            var email_add = $(this).attr("href").replace(/\s*\(.+\)\s*/, "@");
            var email_text = $(this).html();
            $(this).before('<a href="mailto:' + email_add + '" rel="nofollow" title="Email ' + email_add + '">' + email_text + '</a>').remove();
        });
    };

})(jQuery);

$(document).ready(function() {
    $(function() {
        $('.email').mailto();
    });
});

您可以在 jsfiddle 上尝试它。

1

变量电邮 = $(this).html().replace('@', '(at)');


我需要用jQuery来完成它。不幸的是,我可能可以用PHP来完成它,但我有特定的指示需要使用jQuery。 - JacobTheDev

0

我有一个自己制作的 jq-plug-in 可能会有所帮助。 请查看fiddle(working demo)

请记住,插件在 /* -------------------------------------------------------------------------- */ 结束。

使用很简单:

$("#domElementID").jqMailTo({   //  Not all of these options are a must except for the address
    address:    ["Saab@test.net","Volvo@test.net","BMW@test.net"],  // can also be as simple as 1 string, exp -> address:'BMW@test.net',
    label:      'testLabel',
    subject:    'This is a subject',
    body:       'This is a Body section',
    cc:         'aCC@test.net',
    bcc:        'theBCC@test.com'
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接