我怀疑,我可以在这方面作许多改进,尽管此时此刻,这是我所能提供的最佳办法(尽管我认为某些替代办法可能更有效地发挥作用):
var $words = $( p ).text().split( );
for (i in $words) {
if ($words[i].indexOf( http:// ) == 0) {
$words[i] = <a href=" + $words[i] + "> + $words[i] + </a> ;
}
}
$( p ).html($words.join( ));
JS Fiddle demo.
略微改进了上述版本(但好的住所,令人费解):
var punctuation = [ ! ," ", " , , , . ];
$( p ).each(
function(){
$words = $(this).text().split( );
for (i in $words){
if ($.inArray($words[i].charAt(0),punctuation) > -1 && $words[i].indexOf( http:// ) == 1){
alert($words[i]);
}
else if ($.inArray($words[i].charAt($words[i].length - 1),punctuation) > -1 && ($words[i].indexOf( http:// ) == 1 || $words[i].indexOf( http:// ) == 0)){
$words[i] = <a href=" +$words[i].substring(0,$words[i].length-1)+ "> + $words[i].substring(0,$words[i].length-1) + </a> + $words[i].charAt($words[i].length-1);
}
else if ($words[i].indexOf( http:// ) == 0){
$words[i] = <a href=" + $words[i] + "> + $words[i] + </a> ;
}
}
$(this).html($words.join( ));
});
JS Fiddle demo。
我不敢肯定在引用的链接方面可以做些什么,但(例如<编码>”http://google.com>;而且,坦率地说,我认为,这方面的办法可能很远,far。