Javascript 在正文 html 中搜索某些文本的最佳方法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8276113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:24:02  来源:igfitidea点击:

What is the best approach to search some text in body html

javascriptjqueryreplace

提问by Exception

As of now I am using the the javascript search method to replace some text in body HTML like below..

截至目前,我正在使用 javascript 搜索方法来替换正文 HTML 中的一些文本,如下所示..

Suppose my html is

假设我的 html 是

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

Then I am using the below approach

然后我使用下面的方法

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

But I could see slow page search in browsers like IE..
Please suggest me to improve this and also please let know if there are any plugins to search and match a string even if it contains any html tags in it.. Like below

但是我可以在 IE 等浏览器中看到缓慢的页面搜索..
请建议我改进这一点,也请告诉我是否有任何插件可以搜索和匹配字符串,即使它包含任何 html 标签.. 如下所示

 <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")'); 

This will match the DIV and BODY as well.. But here I want to search the html text not parent element... Thanks

这也将匹配 DIV 和 BODY .. 但在这里我想搜索 html 文本而不是父元素......谢谢

回答by Dr.Molle

You should take a look at :

你应该看看:

ranges:(to modify text without overwriting the complete body)

范围:(修改文本而不覆盖整个正文)

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

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



find()/findText()(to create the ranges)

find()/findText()(创建范围)

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

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

(Opera doesn't support find or findText)

(Opera 不支持 find 或 findText)



Regarding to that questiona small modification:

关于这个问题一个小的修改:

<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>

回答by UnLoCo

you will get only the elements that contain the full text
without being a parent to some other element that contains the same text

您将只获得包含全文的元素,
而不是包含相同文本的其他元素的父元素

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

回答by Kannika

I think your code is work well, but your error is you didn't figure out 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);
});

回答by Sudhir Bastakoti

Try this:

尝试这个:

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

Hope it helps

希望能帮助到你