Home >Web Front-end >JS Tutorial >JS implements static page search and highlighting
This article mainly introduces JS to implement static page search and highlighting functions, involving javascript event response, character traversal replacement and dynamic transformation of page element attributes and other related operating techniques. Friends in need can refer to the following
The example in this article describes the JS implementation of static page search and highlighting functions. Share it with everyone for your reference, the details are as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS搜索</title> </head> <body> <input id="key-word" class="key-word" value="请输入搜索内容" /> <button id="search-button">搜索</button> <p id="content" > <p>这是主体内容,有很多内容,很多很多啊啊啊……,比如1234abcd啊啊啊啊</p> <p>这是主体内容,有很多内容,很多很多啊啊啊……,比如1234abcd啊啊啊啊</p> <p>这是主体内容,有很多内容,很多很多啊啊啊……,比如1234abcd啊啊啊啊</p> <p>这是主体内容,有很多内容,很多很多啊啊啊……,比如1234abcd啊啊< d d>啊啊</p> </p> <script> function $(id){ return document.getElementById(id) } var putWordsObj = $('key-word'); putWordsObj.onfocus = function(){ if(this.value == '请输入搜索内容')this.value=''; } putWordsObj.onblur = function(){ if(!this.value)this.value='请输入搜索内容'; } //search $('search-button').onclick = function(){ var content = $('content').innerHTML; var keyWord = $('key-word').value; content = search_do(content, keyWord); $('content').innerHTML = content; //alert(content) } function search_do(content,keyWord){ var keyWordArr = keyWord.replace(/[\s]+/g,' ').split(' '); var re; for(var n = 0; n < keyWordArr.length; n ++) { //re = new RegExp(">[\s\S]*?"+keyWordArr[n]+"[\s\S]*?<\S","gmi"); re = new RegExp(""+keyWordArr[n]+"","gmi"); content = content.replace(re,'<span style="color:#0f0;background-color:#ff0">'+keyWordArr[n]+'</span>'); } return content; } </script> </body> </html>
The operation effect is as follows:
The above is the detailed content of JS implements static page search and highlighting. For more information, please follow other related articles on the PHP Chinese website!