Highlight a word with jQuery

后端 未结 12 1365
粉色の甜心
粉色の甜心 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: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 = '' + word + '';
    		element.innerHTML = element.innerHTML.replace(rgxp, repl);
    			
    			};
    
    	hiliter('dolor', document.getElementById('dolor'));
    });
    .myClass{
    
    background-color:red;
    }
    
    
    	
    		highlight
    		
    		
    	
    		 
    		 
    	
    	
    

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

    Quisque bibendum sem ut lacus. Integer dolor ullamcorper libero. Aliquam rhoncus eros at augue. Suspendisse vitae mauris.

提交回复
热议问题