使用jQuery隐藏电子邮件地址

时间:2011-09-20 18:03:25

标签: jquery email

我知道有插件可以做到这一点,但我需要一些非常具体的东西,我找不到任何东西。

我需要一个只更改mailto链接的jQuery脚本,而不是链接的文本。例如:

<a href="person(at)exammple.com">Person's Email</a>

除了实际的电子邮件地址之外,我还需要在链接中显示其他内容。我尝试使用的脚本来自this site

这是脚本:

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 个答案:

答案 0 :(得分: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 :(得分:1)

var email = $(this).html()。replace('@','(at)');

答案 2 :(得分:0)

我有一个可能有帮助的jq-plug-in。 请参阅fiddle(working demo)

请记住插件以/ *结尾-------------------------------------- ------------------------------------ * /

使用是ez:

$("#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'
});
相关问题