我想知道如何在我的情况下使用此脚本>>> https://stackoverflow.com/a/3890175/1503192 ......
这是我的jsfiddle>>> jsfiddle.net/kZfGV/134/
HTML:
<body onLoad="linkify(inputText)">
https://google.com/<br />
http://google.com/<br />
https://www.google.com/<br />
http://www.google.com/<br />
www.google.com<br />
www.google.com<br />
admin@google.com
</body>
JS:
function linkify(inputText) {
var replacedText, replacePattern1, replacePattern2, replacePattern3;
//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
//Change email addresses to mailto:: links.
replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText;
}
我在jsfiddle中尝试了它,但在我在我的博客中尝试了很多次之前我还是想不通。我也在stackoverflow和谷歌搜索这个,但似乎没有任何作品。我是这个领域的新手。 Plz帮助我们。感谢
答案 0 :(得分:2)
首先,您没有指定函数linkify
中的输入文本是什么。
其次,你确实使用了返回值。
工作示例为http://jsfiddle.net/T7ANY/
脚本看起来像(没有jQuery或任何其他插件)
function linkify(inputText) {
var replacedText, replacePattern1, replacePattern2, replacePattern3;
//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
//Change email addresses to mailto:: links.
replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText;
}
document.body.innerHTML = linkify(document.body.innerHTML)
答案 1 :(得分:2)
http://jsfiddle.net/kZfGV/137/
HTML:
<span class="linkify">https://google.com/</span><br />
<span class="linkify">http://google.com/</span><br />
<span class="linkify">https://www.google.com/</span><br />
<span class="linkify">http://www.google.com/</span><br />
<span class="linkify">www.google.com</span><br />
<span class="linkify">www.google.com</span><br />
<span class="linkify">admin@google.com</span>
的javascript:
$(document).ready(function()
{
$(".linkify").text(function(){
return linkify($(this).text());
});
});
答案 2 :(得分:0)
试试这个:
<!-- jquery and linkify includes here -->
<script>
$( function() {
var body = $('body');
body.html( linkify( body.html() ) );
} )
</script>
<body> ...