自动超链接某个单词(或单词序列)的每个出现到预定义的URL(非模糊);但不显示完整的URL [英] Auto-hyperlink every occurence of a certain word (or word sequence) to predefined URL (non-ambiguous); but not show full URL

查看:160
本文介绍了自动超链接某个单词(或单词序列)的每个出现到预定义的URL(非模糊);但不显示完整的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于: 搜索单词,替换为链接。但是,我不希望看到完整超链接的URL,而是只有最终用户可见的超链接外观。 ---我也无法弄清楚如何使用 replace() -JS-function,就像在下面的帖子中使用的那样:如何替换JavaScript中所有出现的字符串?,针对同一特定问题。 ---也类似于JS问题:将页面上的术语链接到纯JavaScript中的维基百科文章,但我想在那里给出的答案不能预先区分不明确的术语。

Similar to: Search For Words, Replace With Links. However, I would rather not have the full hyperlink's URL visible, but instead have only the appearance of a hyperlink visible to the end-user. --- I also can't figure out how to use the replace()-JS-function, like used in the following post: How to replace all occurrences of a string in JavaScript?, for this same specific issue. --- Also similar to a JS-question: Link terms on page to Wikipedia articles in pure JavaScript, but I guess the answer given there can not, up front, differentiate between ambiguous terms.

在(a)某些HTML页面中,是否可以:

In (a) certain HTML-page(s), is it possible to:


  • 手动预定义;或自动(Cf: Wiki-api )单个单词及其各自(ic Wikipedia)-articles;

  • 的列表,使文档中的每个(此类耦合)单词自动接收到相应的预定义链接(ic维基百科)-article。

  • Predefine manually; or automatically (Cf: Wiki-api) a list of single words and their respective (i.c. Wikipedia)-articles;
  • such that every (such coupled) word in a document automatically receives the predefined link to the respective (i.c. Wikipedia)-article.

示例

(1)无论 cell 这个词出现在哪里,这个词都应该收到链接: ... wiki / cell ,给出以下结果:a cell 只是一个单元格

(1) Wherever the word cell occurs, this word should receive the link: ...wiki/cell, to give the following result: a cell is but a cell.

所以,我真正想要的是模仿任何 Wikip edia-article 本身(虽然我不知道这是否在那里自动化)。

So, what I actually would like is to mimic what is actually happening within any Wikipedia-article itself (although I don't know whether this is automated there).

任何帮助将不胜感激。

推荐答案

从您链接的问题开始,修改它以实现目标非常容易。首先假设我们有一个短语和链接列表,如下所示:

Starting with the question you linked, it's pretty easy to modify it to achieve your goal. So first assuming we have a list of phrase and links like this:

var words = [
    { word: 'foo', link: 'http://www.something.com' },
    { word: 'Something Else', link: 'http://www.something.com/else' ]
];

我们可以迭代该列表替换文档中的任何出现,但包括我们要搜索的单词在我们插入的文本中。我在工具提示中也添加了一个示例,说明你还能做些什么。

We can iterate over that list replacing any occurrence in the document, but including the word we are searching for in the text we are inserting. I've added in a tooltip to this too to show an example of what else you can do.

$(function() {
    $.each(words,
        function() {
            var searchWord = this.word;
            var link = this.link;
            $('body:contains("' + searchWord + '")').each(function() {
                var newHtml = $(this).html().replace(searchWord, 
                    '<a class="wikilink" title="here is a link to Wikipedia" href="'+link+'">' + searchWord + '</a>');
                $(this).html(newHtml);
            });
        }
    );
});

一个工作示例: http://jsfiddle.net/yxhk1fcd/10/

这篇关于自动超链接某个单词(或单词序列)的每个出现到预定义的URL(非模糊);但不显示完整的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆