0

我需要自动链接页面中的 URL。

例如

这是一个 html 内容,包含文本 url www.example.com 和链接的 url <a href='www.example.com'> example </a>。后面可能是 http,例如 [http://www.example.com]

我需要结果是这样的:

这是一个 html 内容,包含文本 url http://www.example.com和链接的 url http://www.example.com。后面可能是 http,例如http://www.example.com

我使用了以下函数,但它不适用于以 www 开头的 url。

    jQuery.fn.autolink = 函数() {
        返回 this.each(function() {
            //以 http://、https:// 或 ftp:// 开头的 URL
            var re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-\{\}$!|]+(?![\w\s? &.\/;#~%"=-]*>))/g;
            $J(this).html($J(this).html().replace(re, '$1'));
        });
    }

4

2 回答 2

0

使用此函数并将您的锚标记文本 url 作为 urlname 和 href 作为链接名传递

function replaceURLWithHTMLLinks(urlname, linkname) {

if (linkname && !linkname.match(/^http([s]?):\/\/.*/)) {
    linkname = 'http://' + linkname
}
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
return linkname.replace(exp,"<a href='$1'>"+urlname+"</a>"); 

}

于 2013-04-10T07:40:37.030 回答
0

尝试这样的事情。这将修改 href 属性和超链接的 html。

$("a").each(function(k,v){
   this.attr("href","http://"+this.attr("href"));
   this.html("http://www."+this.html()+".com");
})
于 2013-04-10T07:38:04.027 回答