使用Jquery在Html链接上建立超链接
我有像Html这样的内容
http://j.mp/eUcRNK
我想像这样在上面的html文本上超链接
http://j.mp/eUcRNK
我怎么能这样做..
$('span').html(function(i,txt){ return $('').text(txt).attr({'target':'_blank', 'href': txt }).addClass('link'); });
演示
根据下面的评论,我想这解决了它。
$('span').html(function(i,txt){ return replaceURLWithHTMLLinks(txt); }); function replaceURLWithHTMLLinks(text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; return text.replace(exp,"https://stackoverflow.com/questions/4655000/make-a-hyper-link-on-html-link-with-jquery/$1"); }
更新小提琴
对于jquery 1.3.2,只需更改jQuery代码。
var span = $('span'); span.html(replaceURLWithHTMLLinks(span.html()));