对我来说,这似乎是jQuery的完美任务。
…这样的事情浮现在我脑海:
// Define: linkify plugin(function($){ var url1 = /(^|<|s)(www..+?..+?)(s|>|$)/g, url2 = /(^|<|s)(((https?|ftp)://|mailto:).+?)(s|>|$)/g, linkifyThis = function () { var childNodes = this.childNodes, i = childNodes.length; while(i--) { var n = childNodes[i]; if (n.nodeType == 3) { var html = $.trim(n.nodevalue); if (html) { html = html.replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(url1, '$1<a href="http://$2">$2</a>$3') .replace(url2, '$1<a href="$2">$2</a>$5'); $(n).after(html).remove(); } } else if (n.nodeType == 1 && !/^(a|button|textarea)$/i.test(n.tagName)) { linkifyThis.call(n); } } }; $.fn.linkify = function () { return this.each(linkifyThis); };})(jQuery);// Usage example:jQuery('div.textbody').linkify();


