vendredi 29 mai 2015

Finding text within Javascript

I'm creating some functions to highlight words on a page.

The way the code works is to take the innerHTML when the page loads, and store this in memory.

I then take a copy of innerHTML - I then find the search words on the copy of innerHTML page, and wrap them with <span id="find-iteration"> tags and replace the contents of the page with the copy of innerHTML. Then, when the user re-hits the search button, I revert the page back to the original innerHTML

How to do I find text (website copy) only words and note words used as values in HTML elements/attributes.

Example, Consider the following code

 <div class="search">This is the search box</div>

Now, I want to perform a search for the word "search". The issue is, it will find it twice.

Without using regular expressions, and only using JavaScript (no jQuery please), how can I detect if the string is part of the HTML tag or not?

Aucun commentaire:

Enregistrer un commentaire