Highlight a word with jQuery

后端 未结 12 1361
粉色の甜心
粉色の甜心 2020-11-22 01:20

I basically need to highlight a particular word in a block of text. For example, pretend I wanted to highlight the word "dolor" in this text:



        
相关标签:
12条回答
  • 2020-11-22 01:37

    Is it possible to get this above example:

    jQuery.fn.highlight = function (str, className)
    {
        var regex = new RegExp(str, "g");
    
        return this.each(function ()
        {
            this.innerHTML = this.innerHTML.replace(
                regex,
                "<span class=\"" + className + "\">" + str + "</span>"
            );
        });
    };
    

    not to replace text inside html-tags like , this otherwise breakes the page.

    0 讨论(0)
  • 2020-11-22 01:39

    Why using a selfmade highlighting function is a bad idea

    The reason why it's probably a bad idea to start building your own highlighting function from scratch is because you will certainly run into issues that others have already solved. Challenges:

    • You would need to remove text nodes with HTML elements to highlight your matches without destroying DOM events and triggering DOM regeneration over and over again (which would be the case with e.g. innerHTML)
    • If you want to remove highlighted elements you would have to remove HTML elements with their content and also have to combine the splitted text-nodes for further searches. This is necessary because every highlighter plugin searches inside text nodes for matches and if your keywords will be splitted into several text nodes they will not being found.
    • You would also need to build tests to make sure your plugin works in situations which you have not thought about. And I'm talking about cross-browser tests!

    Sounds complicated? If you want some features like ignoring some elements from highlighting, diacritics mapping, synonyms mapping, search inside iframes, separated word search, etc. this becomes more and more complicated.

    Use an existing plugin

    When using an existing, well implemented plugin, you don't have to worry about above named things. The article 10 jQuery text highlighter plugins on Sitepoint compares popular highlighter plugins. This includes plugins of answers from this question.

    Have a look at mark.js

    mark.js is such a plugin that is written in pure JavaScript, but is also available as jQuery plugin. It was developed to offer more opportunities than the other plugins with options to:

    • search for keywords separately instead of the complete term
    • map diacritics (For example if "justo" should also match "justò")
    • ignore matches inside custom elements
    • use custom highlighting element
    • use custom highlighting class
    • map custom synonyms
    • search also inside iframes
    • receive not found terms

    DEMO

    Alternatively you can see this fiddle.

    Usage example:

    // Highlight "keyword" in the specified context
    $(".context").mark("keyword");
    
    // Highlight the custom regular expression in the specified context
    $(".context").markRegExp(/Lorem/gmi);
    

    It's free and developed open-source on GitHub (project reference).

    0 讨论(0)
  • 2020-11-22 01:40
    function hiliter(word, element) {
        var rgxp = new RegExp(word, 'g');
        var repl = '<span class="myClass">' + word + '</span>';
        element.innerHTML = element.innerHTML.replace(rgxp, repl);
    }
    hiliter('dolor');
    
    0 讨论(0)
  • 2020-11-22 01:43

    I have created a repository on similar concept that changes the colors of the texts whose colors are recognised by html5 (we don't have to use actual #rrggbb values and could just use the names as html5 standardised about 140 of them)

    colors.js

    $( document ).ready(function() {
    	
    	function hiliter(word, element) {
    		var rgxp = new RegExp("\\b" + word + "\\b" , 'gi'); // g modifier for global and i for case insensitive 
    		var repl = '<span class="myClass">' + word + '</span>';
    		element.innerHTML = element.innerHTML.replace(rgxp, repl);
    			
    			};
    
    	hiliter('dolor', document.getElementById('dolor'));
    });
    .myClass{
    
    background-color:red;
    }
    <!DOCTYPE html>
    <html>
    	<head>
    		<title>highlight</title>
    		
    		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    	
    		 <link href="main.css" type="text/css"  rel="stylesheet"/>
    		 
    	</head>
    	<body id='dolor'>
    <p >
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    </p>
    <p>
        Quisque bibendum sem ut lacus. Integer dolor ullamcorper libero.
        Aliquam rhoncus eros at augue. Suspendisse vitae mauris.
    </p>
     <script type="text/javascript" src="main.js" charset="utf-8"></script>
    	</body>
    </html>

    0 讨论(0)
  • 2020-11-22 01:44
    $(function () {
        $("#txtSearch").keyup(function (event) {
            var txt = $("#txtSearch").val()
            if (txt.length > 3) {
                $("span.hilightable").each(function (i, v) {
                    v.innerHTML = v.innerText.replace(txt, "<hilight>" + txt + "</hilight>");
                });
    
            }
        });
    });
    

    Jfiddle here

    0 讨论(0)
  • 2020-11-22 01:51

    You need to get the content of the p tag and replace all the dolors in it with the highlighted version.

    You don't even need to have jQuery for this. :-)

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