ページでテキストを検索するhtml、css、javascriptのみを使用して検索ボックスを作成しようとしています。一致するテキストが見つかった場合、関数はスパン内のテキストを強調表示する必要があります。
ウェブ(およびstackoverflow)を検索しましたが、自分のニーズに関連する答えを見つけることができませんでした)
私は何をすべきか考えていますが、ある時点で行き詰まります。これが私のコードがこれまでにどのように見えるかです。
<script type="text/javascript">
function init(){
//creates a variable equal to the value entered in the text box
var lookfor = document.getElementById("q").value
//splits the whole page into an array or words
var words = document.body.innerHTML.split(" ");
///loops through the array to get each word
for( var i=0; i<words.length; i++){
}
//This is where I get lost, i dont know how to make it so that what you enter
//in the textbox(lookfor) matches somthing on the page. it will get highlighted. i
//was thinking about using the find object, but im new to javascript and dont know
//how to properly use find yet.
if(lookfor == words[i]) //Find()------
{
'<span class="highlight">' + words + '</span>'
}
else{
alert("Sorry, your search returned no results! Please Try again.")
}
}