简体   繁体   English

高亮显示相似单词字符串中的特定单词

[英]Highlight specific word from similar words string

There are some similar words in a string and I need to highlight only one word out of it. 字符串中有一些相似的单词,我只需要突出显示其中一个单词。 I have index value of this specific word. 我有这个特定单词的索引值。 I'm using Mark JS for this. 我为此使用Mark JS。 I need to do this in filter part of mark JS function. 我需要在标记JS函数的过滤器部分中执行此操作。 But not sure how will I get index value of each word while filtering. 但是不确定在过滤时如何获得每个单词的索引值。

Here is the fiddle to test 这是测试的小提琴

Code JS 代码JS

$(function() {    
    $content = $(".content"),
    currentIndex = 0;        
    var indexVal = 40;
  $('#markBtn').on("click", function() {
    var searchVal = " ipsum ";
    $content.unmark({
      done: function() {
      console.log("unmark")
        $content.mark(searchVal, {          
            "element": "span",
            "className": "mark",
            "accuracy": "partially",
            "iframes": true,
            "ignoreJoiners": true,
            "acrossElements": true,
            "separateWordSearch": true,
            "diacritics": false,
            "filter": function (textNode, foundTerm, totalCounter, counter) {
                console.log("marks - filter  " + textNode+" "+foundTerm);
            //check indexVal with foundTerm and return true
            return true;
            },
            "each": function (node) {
            console.log("marks - each  " + node);
            },
            "done": function() {
                console.log("marks - done  ");            
          }
        });

      }
    });
  });
  });

HTML HTML

<div class="header">
  Mark second word 'ipsum' in the text below.
  <button id="markBtn">Mark Text</button>
</div>
<div class="content">
  <p>Lorem ipsum dolor sit amet, consectetur ipsum adipiscing elit ipsum.</p>
</div>

The filter should return the current index that you want to mark: 过滤器应返回要标记的当前索引:

"filter": function (textNode, foundTerm, totalCounter, counter) {

    console.log("marks - filter  " + textNode+" "+foundTerm);
   var result = totalCounter == currentIndex;
   return result;
},

so if you put currentIndex = 0 it will select the first matching and so on ... 因此,如果您将currentIndex = 0,它将选择第一个匹配项,依此类推...

Refer this Link. 请参阅此链接。

it may help u 它可能对你有帮助

http://jsfiddle.net/sadhique92/HfS7e/1231/ http://jsfiddle.net/sadhique92/HfS7e/1231/

<script>
function highlightSearch() {
    var text = document.getElementById("query").value;
    var query = new RegExp("(\\b" + text + "\\b)", "gim");
    var e = document.getElementById("searchtext").innerHTML;
    var enew = e.replace(/(<span>|<\/span>)/igm, "");
    document.getElementById("searchtext").innerHTML = enew;
    var newe = enew.replace(query, "<span>$1</span>");
    document.getElementById("searchtext").innerHTML = newe;
}
</script>

<style>
#searchtext span{
    background-color:#FF9;
    color:#555;
}

div {
    padding: 10px; 
}

</style>

<div><h2>Find and highlight text in document</h2>
<form action="" method="" id="search" name="search">
<input name="query" id="query" type="text" size="30" maxlength="30">
<input name="searchit" type="button" value="Search" onClick="highlightSearch()">
</form></div>
<div id="searchtext">
<p>JavaScript is the programming language of the Web. The overwhelming majority of
modern websites use JavaScript, and all modern web browsers—on desktops, game
consoles, tablets, and smart phones—include JavaScript interpreters, making Java-
Script the most ubiquitous programming language in history. JavaScript is part of the
triad of technologies that all Web developers must learn: HTML to specify the content
of web pages, CSS to specify the presentation of web pages, and JavaScript to specify
the behavior of web pages. This book will help you master the language.</p>
<p>If you are already familiar with other programming languages, it may help you to know
that JavaScript is a high-level, dynamic, untyped interpreted programming language
that is well-suited to object-oriented and functional programming styles. JavaScript
derives its syntax from Java, its first-class functions from Scheme, and its prototypebased
inheritance from Self. But you do not need to know any of those languages, or
be familiar with those terms, to use this book and learn JavaScript.</p>
<p>The name "JavaScript" is actually somewhat misleading. <span>Except</span> for a superficial syntactic
resemblance, JavaScript is completely different from the Java programming language.
And JavaScript has long since outgrown its scripting-language roots to become
a robust and efficient general-purpose language. The latest version of the language (see
the sidebar) defines new features for serious large-scale software development.</p>
</div>

在此处输入图片说明 Your question: not sure how will I get index value of each word while filtering? 您的问题:不确定在过滤时如何获取每个单词的索引值?

The Mark JS documentation read as follows: Mark JS文档的内容如下:

filter function: A callback to filter or limit matches. filter函数:一个用于过滤或限制匹配项的回调。 It will be called for each match and receives the following parameters: 每次匹配都会调用它,并接收以下参数:

  1. The text node which includes the match 包含匹配项的文本节点
  2. The term that has been found 已找到的术语
  3. A counter indicating the total number of all marks at the time of the function call 一个计数器,指示函数调用时所有标记的总数
  4. A counter indicating the number of marks for the term 一个计数器,指示该词的分数
The function must return false if the mark should be stopped, otherwise true. 如果应停止标记,则该函数必须返回false,否则返回true。

In order words: the for forth parameter is the counter(index) of each word found. 按顺序排列的单词:forward参数是找到的每个单词的计数器(索引)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM