2009-07-13 2 views
1

Ich habe eine HTML-Tabelle mit einer Spalte E-Mail-Adressen; spambot Ausgaben beiseite, möchte ich jedes <td> Element in dieser Spalte zu einem Mailto: Link konvertieren.So verwenden Sie jQuery zum Konvertieren von Text in E-Mail-Links

Hier ist, was ich habe:

$("table.data tbody tr td:last").each(function() { 
    var email = $(this).val(); 
    $(this).html('<a href="mailto:' + email + '">' + email + '</a>'); 
}); 

Und das ist die Tabelle (Probe):

<tr> 
    <td>Joe</td> 
    <td>Shmoe</td> 
    <td>[email protected]</td> 
</tr> 

Was falsch los ist?

Antwort

2

Ihre jQuery-Selektor ist falsch.

Wenn Sie tun 'table.data tbody tr td: last' wird nur die letzte Zelle der letzten Zeile ausgewählt.

Was Sie tun müssen, ist so etwas wie:

$(document).ready(
    function() 
    { 
     //For each row... 
     $('table.data tbody tr').each(
      function() 
      { 
       //...find the last cell. 
       var lastCell = $(this).find('td:last'); 
       $(lastCell).html('<a href="mailto:' + $(lastCell).text() + '">' + $(lastCell).text() + '</a>'); 
      } 
     ); 
    } 
); 

Arbeits Demo: http://jsbin.com/umiza Code: http://jsbin.com/umiza/edit

+0

perfekt funktioniert! +1 – peehskcalba

1

Sie .text verwenden sollten() statt .val():

$("table.data tbody tr td:last").each(function() { 
    var email = $(this).text(); 
    $(this).html('<a href="mailto:' + email + '">' + email + '</a>'); 
}); 
Verwandte Themen