jQuery - 'href'插件?

时间:2014-03-07 16:17:20

标签: javascript jquery

如果有人知道插件谁可以在div上找到链接并使其可点击?我有文字div:

<div id="mytext">Wow, please go to http://www.google.com - this is my home page ;)</div>

我想制作自动点击链接。

1 个答案:

答案 0 :(得分:0)

易于实施:

Fiddle

var url_pattern = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;

var content = $("#mytext").text();
var newContent = content.replace(url_pattern, function (link) {
    return '<a href="' + link + '" target="_blank">' + link + '</a>';
});
$('#mytext').html(newContent);