jquery分析文本裡url或郵件地址為真實連結的方法
本文執行個體講述了jquery分析文本裡url或郵件地址為真實連結的方法。分享給大家供大家參考。具體如下:
這段代碼可以分析出文本裡的所有超級連結,包含郵件、url、#連結等等,並分別輸出為真實連結地址
?
| 1 2 3 4 5 6 7 8 9 10 11 |
$.fn.tweetify = function() { this.each(function() { $(this).html( $(this).html() .replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>') .replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>') .replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>') ); }); return $(this); } |
用法:
代碼如下:
$("p").tweetify();
原始文本:
代碼如下:
@seanhood have you seen this http://icanhascheezburger.com/ #lol
分析以後:
?
| 1 2 3 |
<p><a href="http://twitter.com/seanhood">@seanhood</a> have you seen this <a href="http://icanhascheezburger.com/">http://icanhascheezburger.com/</a> <a href="http://search.twitter.com/search?q=%23lol">#lol</a></p> |
希望本文所述對大家的jQuery程式設計有所協助。