Tuesday 15 September 2015

html - Trying to find keywords and italicize them using JavaScript -


I tried to create something that would search the whole webpage for specific keywords and use the pure JavaScript Italicize them for I have not tried anything with Javascript lately, so I do not know why this is not working. I have tried to googling, but for my problem there was no particular benefit (everything that I found was only trying to change the class of an element as opposed to the original CSS property having had). I really want to know why why is not working here is what I have done so far:

  & lt; Html & gt; & Lt; Top & gt; & Lt; Script & gt; Var str = document.getElementsByTagName ("*"). InnerHTML; Var n = str.match (/ (Nautilus | Capture Nemo) / i); N.style.fontStyle = "Italic"; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; Nautilus was a ship powered by Captain Nemo & Lt; / Body & gt; & Lt; / Html & gt;   

The result should be: There was a ship run by Nautilus Captain Nemo .

My UN-experienced logic tells me that I should try to parse the entire document as a large string, then using that string it will match the word using the rags, then Style.fontStyle .

Now, as I said, because I have been busy, I have not touched Javascript or even Jacques for a while, so I can be closed, but any help would be greatly appreciated. !

n Here's just a string, this is not a node on the page and On the page, there may be styles in only one node which are different from their parents. You can not just tell the string to be italic. This is just a string and it does not have any associated presentation information.

To do this you should:

  1. Find the text
  2. Insert the HTML string back into the document

    or

    In JS:

      // get all body contents as a string Str = document.body.inner HTML; // Find all the magic words and give them the & lt; Em> Tag Vertical Results = str.replace (/ (Nautilus | Capture Nemo) / ig, " em $ 1 "); // Set up all body content HTML with new processed content document. Nobody.innerHTML = Results;   

    See it working here:

No comments:

Post a Comment