简体   繁体   中英

Add mailto: with jQuery?

<table id="here" border="1">
    <tr><td>London</td><td>london@us.uk</td><td>aaa</td><td>aaa</td></tr>
    <tr><td>Manchester</td><td>manchester@us.uk</td><td>aaa</td><td>aaa</td></tr>
    <tr><td>Liverpool</td><td>liverpool@us.uk</td><td>aaa</td><td>aaa</td></tr>
    <tr><td>Ipswich</td><td>ipswich@us.uk</td><td>aaa</td><td>aaa</td></tr>
</table>

Is possible add link mailto: for second columns with email addresses with jQuery (not modify HTML)? If yes, how?

http://jsfiddle.net/zwsMD/1/

You could just replace the contents of each second td with an a element with a mailto: href: http://jsfiddle.net/zwsMD/5/ .

​$("#here td:nth-child(2)").each(function() {
    var email = $(this).text();

    // replace contents
    $(this).html(
        $("<a>").attr("href", "mailto:" + email).text(email)
    );
});​​​​​​​​​​​​​​​​​​​​

You could do something like this

​$('td:nth-child(2)').each(function(){
   var text = $(this).text();
    var href = "mailto:"+text;
    var $a = $('<a>', { href: href, text: text});
    $(this).text('').append($a);
});​​​​​​​​​​​​​​​​​​​​

fiddle here http://jsfiddle.net/zwsMD/6/

Assuming it's always the second td of each row, you could iterate over those elements and wrap the contents in an a element:

$("#here td:nth-child(2)").each(function() {
    $(this).contents().wrap("<a href='mailto:" + $(this).text() + "'>");
});​

Here's a working example .

You will need to loop around every row, find the cell you want and wrap a link around the content. You can use wrapInner for this.

$("#here tr").each(function() {
    var td = $(this).children().eq(1);
    var email = "mailto:" + td.text();
    td.wrapInner($("<a>").prop("href", email));
});​

Live example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM