使关键字自动全局链接
is there a way to make a every instance of a word automatically turn into a link?
so for instance, everytime I write "apple", it is automatically formatted to <a href="www.apple.com" class="whatever" target="_blank">apple</a>
I'm assuming i could use javascript or possibly jquery.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常非常简单的例子...
jQuery
html
演示
very very simple example...
jQuery
html
demo
这是一个简单的 jQuery 插件,应该可以解决这个问题。它只会选择文本节点,因此如果您有一个类
apple
或 idapple
的元素,它不会被替换。此外,如果您有一个链接apple
它不会被替换(可能比您需要的多一点,但我想我'无论如何都要发布):用法:
工作示例:http://jsfiddle.net/andrewwhitaker/VmHjJ/
请注意,由于使用
$("*")
选择器,这可能会很快变得低效。如果可能,您应该将其替换为更具体的内容(或完全删除.find("*").andSelf()
部分,并向插件传递更具体的选择器)。Here's a simple jQuery plugin that should do the trick. It will only select text nodes so that if you have an element with a class
apple
or idapple
it won't get replaced. Additionally, if you have a link<a href="#">apple</a>
it won't get replaced (might be a little more than you need, but I thought I'd post it anyway):Usage:
Working example: http://jsfiddle.net/andrewwhitaker/VmHjJ/
Note that this could become inefficient rather quickly due to the use of the
$("*")
selector. If possible, you should replace it with something more specific (or remove the.find("*").andSelf()
portion entirely and pass the plugin a more specific selector).