本文實例講述了jquery分析文本里url或郵件地址為真實鏈接的方法。分享給大家供大家參考。具體如下:
這段代碼可以分析出文本里的所有超級鏈接,包含郵件、url、#鏈接等等,并分別輸出為真實鏈接地址
$.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 ) .replace(/(^|/s)@(/w+)/g,'$1<a ) ); }); return $(this);}
用法:
原始文本:
分析以后:
<p><a >@seanhood</a> have you seen this<a >http://icanhascheezburger.com/</a><a >#lol</a></p>
希望本文所述對大家的jQuery程序設計有所幫助。
新聞熱點
疑難解答