简体   繁体   English

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

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

As of now I am using 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..但是我可以在 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请建议我改进这一点,也请让我知道是否有任何插件可以搜索和匹配字符串,即使它包含任何 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 文本而不是父元素......谢谢

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 IE: http://msdn.microsoft.com/en-us/library/ms535872%28v=vs.85%29.aspx
Others: https://developer.mozilla.org/en/DOM/range其他: 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 IE: http://msdn.microsoft.com/en-us/library/ms536422%28v=vs.85%29.aspx
Others: https://developer.mozilla.org/en/DOM/window.find其他: https : //developer.mozilla.org/en/DOM/window.find

(Opera doesn't support find or findText) (Opera 不支持 find 或 findText)


Regarding to that question a 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>

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
                  }
               );

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);
});

Try this:尝试这个:

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

Hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较文字:什么是最好的方法 - Comparing text: what is the best approach 通过名称搜索用户的最佳方法是什么? - What would be the best approach to search user by the name? 什么是用于处理HTML中的复杂的形式,最好的办法? - What is the best approach for handling a complex form in html? 确认 html 表单字段的最佳方法是什么? - What is the best approach for confirming html form fields? 什么是HTML5视频的最佳方法? - What would be the best approach to HTML5 video? 使用点击刷新表格正文的最佳方法是什么 - What is the best approach to refresh table body using on click 关于性能的最佳方法是什么:setInterval()或身体上的全局事件? - What is a best approach regarding performance: setInterval() or a global event on body? 什么是在html数据属性中存储值的最佳方法 - what is the best approach to store values in html data attribute 在famo.us中,确定表面上文本元素大小的最佳方法是什么? - In famo.us, what is the best approach to sizing text elements on surfaces? 实现简单文本搜索的最佳方法是什么 - What is the best way to implement simple text search
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM