What is the best approach to finding any text in body html

I am currently using the javascript search method to replace some text in body text, as shown below.

Assume my html

 <body>
      <div> I am in body...</div>
 </body>

Then I use the approach below

 var body = $(body);
 var text = body.html();
 text = text.replace('I am in body...','Yes');
 body.html(text);

But I could see a slow page search in browsers such as IE ..
Please suggest me improve this, and also please let me know if there are any plugins for searching and matching a string, even if it has any html tags. As below

 <body>
       <div><span>I </span> am in body...</div>
 </body>

With the current method, I cannot match this.
If i use

  $('*:contains("some search text string")'); 

It will also match DIV and BODY. But here I want to look for html text, not the parent element ... Thanks

+5
4

:

: ( )

IE: http://msdn.microsoft.com/en-us/library/ms535872%28v=vs.85%29.aspx
: https://developer.mozilla.org/en/DOM/range


find()/findText() ( )

IE: http://msdn.microsoft.com/en-us/library/ms536422%28v=vs.85%29.aspx
: https://developer.mozilla.org/en/DOM/window.find

(Opera find findText)


question :

<html>
<head>

  <script>
  function fx(a,b)
  {
    if(window.find)
    {
      while(window.find(a))
      {

        var rng=window.getSelection().getRangeAt(0);
        rng.deleteContents();

        rng.insertNode(document.createTextNode(b));

      }
    }
    else if(document.body.createTextRange)
    {
      var rng=document.body.createTextRange();
      while(rng.findText(a))
      {
        rng.pasteHTML(b);
      }
    }
  }
  </script>
</head>
<body onload="fx('I am in body...','Yes')">
<div>Oh <span>I </span>am in body...<i>, wonderful</i></div>
</body>
</html>
+4

,
  - ,

var str = "some search text string";
var elements = $("*:contains('"+ str +"')").filter(
                  function(){
                      return $(this).find("*:contains('"+ str +"')").length == 0
                  }
               );
+1

I think your code is working fine, but your mistake is that you did not define the "body" content.

Here is your code:

$(document).ready(function(){
    var body = $("body");    // your miss this
    var text = body.html();
    text = text.replace('I am in body...','Yes');
    body.html(text);
});
+1
source

Try the following:

$ (function () {
    var foundin = $ ('body: contains ("some search text string")');
});

Hope this helps

0
source

All Articles