全局自动将关键字设为链接 [英] make keyword into link automatically, globally
本文介绍了全局自动将关键字设为链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让一个单词的每个实例自动变成一个链接?
is there a way to make a every instance of a word automatically turn into a link?
所以,例如,每当我写apple时,它会自动生成格式化为< a href =www.apple.comclass =whatevertarget =_ blank> apple< / a>
so for instance, everytime I write "apple", it is automatically formatted to <a href="www.apple.com" class="whatever" target="_blank">apple</a>
我假设我可以使用javascript或者jquery。
I'm assuming i could use javascript or possibly jquery.
谢谢!
推荐答案
非常简单的例子......
very very simple example...
jQuery
var span = $('span');
span.html(function(i,html){
replaceTextWithHTMLLinks(html);
}); // jQuery version 1.4.x
function replaceTextWithHTMLLinks(text) {
var exp = /(apple)/ig;
return text.replace(exp,"<a class='link' href='http://www.$1.com' target='_blank' >$1</a>");
}
html
<span>
An apple a day, makes 7 apples a week!
</span>
这篇关于全局自动将关键字设为链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文