Highlighting text in document (JavaScript) Efficiently

后端 未结 3 1080
不思量自难忘°
不思量自难忘° 2020-12-20 07:50

How can I (efficiently - not slowing the computer [cpu]) highlight a specific part of a page?

Lets say that my page is as so:

         


        
相关标签:
3条回答
  • 2020-12-20 08:18
    <html>
    <head>
    </head>
    <body>
    <p id="myText">"My generic words would be selected here" !.</p>
    <script>
    //highlight code here
    var textToHighlight = 'selected here" !';
    var text = document.getElementById("myText").innerHTML
    document.getElementById("myText").innerHTML = text.replace(textToHighlight, '<span style="color:red">'+textToHighlight+'</span>');
    //what sould I write here?
    </script>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-20 08:24

    I've adapted the following from my answers to several similar questions on SO (example). It's designed to be reusable and has proved to be so. It traverses the DOM within a container node you specify, searching each text node for the specified text and using DOM methods to split the text node and surround the relevant chunk of text in a styled <span> element.

    Demo: http://jsfiddle.net/HqjZa/

    Code:

    // Reusable generic function
    function surroundInElement(el, regex, surrounderCreateFunc) {
        // script and style elements are left alone
        if (!/^(script|style)$/.test(el.tagName)) {
            var child = el.lastChild;
            while (child) {
                if (child.nodeType == 1) {
                    surroundInElement(child, regex, surrounderCreateFunc);
                } else if (child.nodeType == 3) {
                    surroundMatchingText(child, regex, surrounderCreateFunc);
                }
                child = child.previousSibling;
            }
        }
    }
    
    // Reusable generic function
    function surroundMatchingText(textNode, regex, surrounderCreateFunc) {
        var parent = textNode.parentNode;
        var result, surroundingNode, matchedTextNode, matchLength, matchedText;
        while ( textNode && (result = regex.exec(textNode.data)) ) {
            matchedTextNode = textNode.splitText(result.index);
            matchedText = result[0];
            matchLength = matchedText.length;
            textNode = (matchedTextNode.length > matchLength) ?
                matchedTextNode.splitText(matchLength) : null;
            surroundingNode = surrounderCreateFunc(matchedTextNode.cloneNode(true));
            parent.insertBefore(surroundingNode, matchedTextNode);
            parent.removeChild(matchedTextNode);
        }
    }
    
    // This function does the surrounding for every matched piece of text
    // and can be customized  to do what you like
    function createSpan(matchedTextNode) {
        var el = document.createElement("span");
        el.style.backgroundColor = "yellow";
        el.appendChild(matchedTextNode);
        return el;
    }
    
    // The main function
    function wrapText(container, text) {
        surroundInElement(container, new RegExp(text, "g"), createSpan);
    }
    
    wrapText(document.body, "selected here");
    
    0 讨论(0)
  • 2020-12-20 08:39

    Use this in combination with this and you should be pretty ok. (It is almost better than trying to implement selection / selection-highlighting logic yourself.)

    0 讨论(0)
提交回复
热议问题