2011-09-20 101 views
7

我知道有這樣做的插件,但我需要一些非常具體的東西,我找不到任何東西。用jQuery隱藏電子郵件地址

我需要一個jQuery的腳本,只改變mailto鏈接,而不是鏈接的文本。例如:

<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>標籤之間的東西「電子郵件」變量,但我不知道什麼。我對jQuery仍然很陌生。

謝謝。

回答

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

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

+0

不幸的是我需要用jQuery來做到這一點。我大概可以用PHP來做,但我有具體的指示,它需要是jQuery。 – JacobTheDev

+3

看起來像jQuery給我... –

0

我有JQ-插件,我做了可能的幫助。 查看fiddle(working demo)

請記住插件在/ * --------------------------------- ----------------------------------------- */

要使用ez:

$("#domElementID").jqMailTo({ // Not all of these options are a must except for the address 
    address: ["[email protected]","[email protected]","[email protected]"], // can also be as simple as 1 string, exp -> address:'[email protected]', 
    label:  'testLabel', 
    subject: 'This is a subject', 
    body:  'This is a Body section', 
    cc:   '[email protected]', 
    bcc:  '[email protected]' 
}); 
相關問題