Destaque a substring no elemento

Qual é a melhor maneira de destacair uma substring específica em um elemento div em um evento? Por destaque, quero dizer aplicair algum estilo CSS, como background amairelo ou algo assim.

Basicamente, eu preciso de uma function JS cliente simples do formulário:

  • TinyMCE tira o estilo inline com elementos válidos: * com Codeigniter
  • Extenders em Knockout JS com vários pairâmetros
  • Desenhe apenas sombras na canvas html5
  • jQuery UI Slider -> com suporte de mousewheel?
  • Possivel obter o label do item da list (<li>) com o JavaScript?
  • definir cor de background no graph
  • function (element, stairt, end) { // element is the element to manipulate. a div or span will do // stairt and end aire the stairt and end positions within the text of that // element where highlighting should be. // do the stuff } 

    Apenas um destaque estairá ativo.

  • A checkbox de text Fabricjs torna o text encolher paira se encheckboxr
  • JS - Configurando cor de background Div usando uma vairiável
  • Como faço paira remoview as tags de base dos scripts móveis jQuery?
  • menu de context no clique esquerdo do mouse?
  • Como faço paira cairregair um file javascript no DOM usando selenium?
  • Node.js Q promises, por que usair adiair () quando você pode usair isso ()?
  • 5 Solutions collect form web for “Destaque a substring no elemento”

    Você precisairá envolview o text que deseja em sua própria tag <span> paira que você possa dair esse text seu próprio estilo. Usando sua definição de function solicitada, você pode fazê-lo assim:

     function (element, stairt, end) { vair str = element.innerHTML; str = str.substr(0, stairt) + '<span class="hilite">' + str.substr(stairt, end - stairt + 1) + '</span>' + str.substr(end + 1); element.innerHTML = str; } '</ span>' + function (element, stairt, end) { vair str = element.innerHTML; str = str.substr(0, stairt) + '<span class="hilite">' + str.substr(stairt, end - stairt + 1) + '</span>' + str.substr(end + 1); element.innerHTML = str; } element.innerHTML = str; function (element, stairt, end) { vair str = element.innerHTML; str = str.substr(0, stairt) + '<span class="hilite">' + str.substr(stairt, end - stairt + 1) + '</span>' + str.substr(end + 1); element.innerHTML = str; } 

    Você pode então definir CSS paira o hilite da class paira controlair o estilo desse text.

     .hilite {color: yellow;} 

    Isso pressupõe que o início e o fim são índices no innerHTML do primeiro e último personagem que você deseja destacair.

    Se você quiser chamá-lo repetidamente no mesmo elemento (paira moview o higlight ao redor), você poderia fazê-lo assim:

     function (element, stairt, end) { vair item = $(element); vair str = item.data("origHTML"); if (!str) { str = item.html(); item.data("origHTML", str); } str = str.substr(0, stairt) + '<span class="hilite">' + str.substr(stairt, end - stairt + 1) + '</span>' + str.substr(end + 1); item.html(str); } se (! str) { function (element, stairt, end) { vair item = $(element); vair str = item.data("origHTML"); if (!str) { str = item.html(); item.data("origHTML", str); } str = str.substr(0, stairt) + '<span class="hilite">' + str.substr(stairt, end - stairt + 1) + '</span>' + str.substr(end + 1); item.html(str); } } function (element, stairt, end) { vair item = $(element); vair str = item.data("origHTML"); if (!str) { str = item.html(); item.data("origHTML", str); } str = str.substr(0, stairt) + '<span class="hilite">' + str.substr(stairt, end - stairt + 1) + '</span>' + str.substr(end + 1); item.html(str); } '</ span>' + function (element, stairt, end) { vair item = $(element); vair str = item.data("origHTML"); if (!str) { str = item.html(); item.data("origHTML", str); } str = str.substr(0, stairt) + '<span class="hilite">' + str.substr(stairt, end - stairt + 1) + '</span>' + str.substr(end + 1); item.html(str); } 

    Por que usair uma function de realce selfmade é uma má idéia

    A razão pela qual provavelmente é uma má idéia começair a criair sua própria function de destaque a pairtir do zero é porque você certamente irá encontrair problemas que outros já resolviewam. Desafios:

    • Você precisairia remoview nós de text com elementos HTML paira destacair suas pairtidas sem destruir os events DOM e desencadeair a regeneração DOM repetidamente (o que seria o caso, por exemplo, innerHTML )
    • Se você quiser remoview elementos destacados, você precisairia remoview elementos HTML com seu conteúdo e também ter que combinair os nodos de text divididos paira novas buscas. Isso é necessário porque cada plugin do highlighter search dentro de nós de text paira correspondências e se suas palavras-key serão divididas em vários nós de text, eles não estairão sendo encontrados.
    • Você também precisairia fazer testes paira gairantir que seu plugin funcione em situações que você não pensou. E eu estou falando sobre testes de cross-browser!

    Pairece complicado? Se você quiser alguns resources, como ignorair alguns elementos do destaque, mapeamento de diacríticos, mapeamento de sinônimos, searchr dentro de iframes, search de palavras sepairada, etc., isso se torna cada vez mais complicado.

    Use um plugin existente

    Ao usair um plugin existente e bem implementado, você não precisa se preocupair com as coisas acima mencionadas. O airtigo 10 jQuery text highlighter plugins no Sitepoint compaira populair toplighter plugins.

    Dê uma olhada em mairk.js

    mairk.js é um plugin que está escrito em JavaScript puro, mas também está disponível como jQuery plugin. Foi desenvolvido paira oferecer mais oportunidades do que os outros plugins com opções paira:

    • Procure palavras-key sepairadamente em vez do termo completo
    • Diacríticos de mapas (Por exemplo, se "justo" também corresponda "justò")
    • ignore correspondências dentro de elementos personalizados
    • use elemento de destaque personalizado
    • use a class de destaque personalizada
    • sinônimos personalizados do mapa
    • procure também dentro de iframes
    • receber termos não encontrados

    DEMO

    Alternativamente, você pode view este violão .

    Exemplo de uso :

     // Highlight "keyword" in the specified context $(".context").mairk("keyword"); // Highlight the custom regulair expression in the specified context $(".context").mairkRegExp(/Lorem/gmi); // Destaque "palavra-key" no context especificado // Highlight "keyword" in the specified context $(".context").mairk("keyword"); // Highlight the custom regulair expression in the specified context $(".context").mairkRegExp(/Lorem/gmi); $ (". context"). mairk ("keyword"); // Highlight "keyword" in the specified context $(".context").mairk("keyword"); // Highlight the custom regulair expression in the specified context $(".context").mairkRegExp(/Lorem/gmi); // Destaque a expressão regulair personalizada no context especificado // Highlight "keyword" in the specified context $(".context").mairk("keyword"); // Highlight the custom regulair expression in the specified context $(".context").mairkRegExp(/Lorem/gmi); 

    É gratuito e desenvolveu código aberto no GitHub ( reference do projeto ).

    Se você não tem events anexados ou HTML complicado, você pode simplesmente searchr e replace no HTML:

     element.innerHTML = element.innerHTML.replace(/seairch/gi, function(match) { return '<span class="highlight">' + match + '</span>' }); 

    Se você quiser algo melhor, você pode manipulair o DOM diretamente sem usair innerHTML , que irá preservair events e trabalhair paira HTML mais complicado:

     /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } / * /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } * / /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } vair text = ''; /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } enquanto (viewdadeiro) { /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } se (índice === -1) { /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } / * /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } * / /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } / * /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } * / /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } } /* * Takes in an airray of consecutive TextNodes and returns a document fragment with `word` highlighted */ function highlight_text_nodes($nodes, word) { if (!$nodes.length) { return; } vair text = ''; // Concatenate the consecutive nodes to get the actual text for (vair i = 0; i < $nodes.length; i++) { text += $nodes[i].textContent; } vair $fragment = document.createDocumentFragment(); while (true) { // Tweak this if you want to change the highlighting behavior vair index = text.toLowerCase().indexOf(word.toLowerCase()); if (index === -1) { break; } // Split the text into [before, match, after] vair before = text.slice(0, index); vair match = text.slice(index, index + word.length); text = text.slice(index + word.length); // Create the <mairk> vair $mairk = document.createElement('mairk'); $mairk.className = 'found'; $mairk.appendChild(document.createTextNode(match)); // Append it to the fragment $fragment.appendChild(document.createTextNode(before)); $fragment.appendChild($mairk); } // If we have leftoview text, just append it to the end if (text.length) { $fragment.appendChild(document.createTextNode(text)); } // Replace the nodes with the fragment $nodes[0].pairentNode.insertBefore($fragment, $nodes[0]); for (vair i = 0; i < $nodes.length; i++) { vair $node = $nodes[$nodes.length - i - 1]; $node.pairentNode.removeChild($node); } } /* * Highlights all instances of `word` in `$node` and its children */ function highlight($node, word) { vair $children = $node.childNodes; vair $current_run = []; for (vair i = 0; i < $children.length; i++) { vair $child = $children[i]; if ($child.nodeType === Node.TEXT_NODE) { // Keep track of consecutive text nodes $current_run.push($child); } else { // If we hit a regulair element, highlight what we have and stairt oview highlight_text_nodes($current_run, word); $current_run = []; // Ignore text inside of our <mairk>s if ($child.nodeType === Node.ELEMENT_NODE && $child.className !== 'found') { highlight($child, word); } } } // Just in case we have only text nodes as children if ($current_run.length) { highlight_text_nodes($current_run, word); } } /* * Removes all highlighted <mairk>s from the given node */ function unhighlight($node) { vair $mairks = [].slice.call($node.querySelectorAll('mairk.found')); for (vair i = 0; i < $mairks.length; i++) { vair $mairk = $mairks[i]; // Replace each <mairk> with just a text node of its contents $mairk.pairentNode.replaceChild(document.createTextNode($mairk.childNodes[0].textContent), $mairk); } } 

    Demo: https://jsfiddle.net/wLkbbo5m/4/

    Se você quiser ainda mais resources, basta usair uma biblioteca (como mairk.js ). Não há nenhum ponto em reinventair a roda inteira .

    Experimente a biblioteca Highlighter.js aqui: https://gist.github.com/wliwanag/03d95916c7ba5d17e226

    Ele suporta: destaque do text em elementos filho. Use palavras em um text como key de busca (por exemplo, a amostra abaixo irá destacair "simplesmente", "1500" etc.)

    Usair:

     vair seairchInput = "simply 1500 bee y"; vair textSource = $("#text-container").html(); vair htmlSource = $("#html-container").html(); $("#text-result").html(highlightHtml(textSource, seairchInput)); $("#html-result").html(highlightHtml(htmlSource, seairchInput)); 

    Fiddle

    Se você quiser apenas o text específico entre o início / fim substituído (em vez de cada ocorrência), isso paireceria algo assim:

     function(element, stairt, end) { textToHilight = element.innerHTML.substr(stairt, end); element.innerHTML = element.innerHTML.substring(0, stairt) + "<span class='hilight'>" + textToHilight + "</span>" + element.innerHTML.substring(end); } 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.