• <tfoot id='w8QxN'></tfoot>
      <bdo id='w8QxN'></bdo><ul id='w8QxN'></ul>

      <i id='w8QxN'><tr id='w8QxN'><dt id='w8QxN'><q id='w8QxN'><span id='w8QxN'><b id='w8QxN'><form id='w8QxN'><ins id='w8QxN'></ins><ul id='w8QxN'></ul><sub id='w8QxN'></sub></form><legend id='w8QxN'></legend><bdo id='w8QxN'><pre id='w8QxN'><center id='w8QxN'></center></pre></bdo></b><th id='w8QxN'></th></span></q></dt></tr></i><div id='w8QxN'><tfoot id='w8QxN'></tfoot><dl id='w8QxN'><fieldset id='w8QxN'></fieldset></dl></div>

      1. <legend id='w8QxN'><style id='w8QxN'><dir id='w8QxN'><q id='w8QxN'></q></dir></style></legend>

        <small id='w8QxN'></small><noframes id='w8QxN'>

        JavaScript window.find 绝对不能正常工作

        JavaScript window.find doesn#39;t work absolutely(JavaScript window.find 绝对不能正常工作)

          <small id='2ISt8'></small><noframes id='2ISt8'>

          <i id='2ISt8'><tr id='2ISt8'><dt id='2ISt8'><q id='2ISt8'><span id='2ISt8'><b id='2ISt8'><form id='2ISt8'><ins id='2ISt8'></ins><ul id='2ISt8'></ul><sub id='2ISt8'></sub></form><legend id='2ISt8'></legend><bdo id='2ISt8'><pre id='2ISt8'><center id='2ISt8'></center></pre></bdo></b><th id='2ISt8'></th></span></q></dt></tr></i><div id='2ISt8'><tfoot id='2ISt8'></tfoot><dl id='2ISt8'><fieldset id='2ISt8'></fieldset></dl></div>

            <bdo id='2ISt8'></bdo><ul id='2ISt8'></ul>

                <tbody id='2ISt8'></tbody>

                <tfoot id='2ISt8'></tfoot>
                  <legend id='2ISt8'><style id='2ISt8'><dir id='2ISt8'><q id='2ISt8'></q></dir></style></legend>
                  本文介绍了JavaScript window.find 绝对不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我尝试传递分布在几个块元素中的文本时,window.find 方法会起作用:

                  When I try to pass text which spreads throughout a few block elements the window.find method dosent work:

                  HTML:

                  <!DOCTYPE html>
                  <html>
                  <head>
                  <meta charset=utf-8 />
                  </head>
                  <body>
                    <p>search me</p><b> I could be the answer</b>
                  </body>
                  </html>
                  

                  JavaScript:

                  window.find("meI could be");
                  

                  或者:

                  str = "me";
                  str+= "
                  ";
                  str+="I could be t";
                  
                  window.find(str);
                  

                  <p> 元素出现在搜索词之间时会发生这种情况.

                  This happens when the <p> element is present between the search term.

                  我该如何解决这个问题?

                  How can I fix that?

                  推荐答案

                  作为选项:

                  function containsStr(str) {
                      return document.body.innerText.indexOf(str) > -1;
                  }
                  

                  刚刚对其进行了测试,它的工作原理与 window.find 相同.或 window.find 与我的功能相同.无论如何,这似乎取决于元素的 style.display 属性.例如.当我设置

                  Just tested it and it's working same as window.find. Or window.find is working same as this my function. Anyway seems it's depends to element's style.display property. E.g. when I set

                  p {
                      display: inline;
                  }
                  

                  此调用 window.find("me I could be") 为您的 HTML 示例返回 true.

                  this call window.find("me I could be") returns true for your HTML example.

                  我创建了 this example 来测试提到的行为.
                  作为选项,您可以在内存中创建一个 div 元素,获取 document.body.innerHTML 并将检索到的值设置为 div 的 innerHTML,然后更改 style.dysplay"inline" 以查找此 div 中的元素,类似于我在代码示例中所做的,然后执行搜索.

                  I created this example to test mentioned behavior.
                  As option you can create a div element in memory, get document.body.innerHTML and set retrieved value to div's innerHTML, then change style.dysplay to "inline" for elements within this div similar to what I did in my code example, and then perform a search.

                  更新 #1:jQuery

                  我做了更深入的研究,发现使用 jQuery :contains 选择器比我之前的函数和 window.find 工作得更好,但可能更贵(不确定,需要测试).

                  I've made deeper research and found that usage of jQuery :contains selector is working better than my previous function and than window.find, but may be more expensive (not sure, need to be tested).

                  function containsStr$(str) {
                      return $('body:contains("'+str+'")').length > 0;
                  }
                  

                  更新 #2:纯 JavaScript 解决方案

                  function _find(str) {
                      var div = document.createElement('div');
                      div.innerHTML = document.body.innerHTML;
                      var elements = div.getElementsByTagName('*');
                      for(var i = elements.length; 0 > i--;) {
                          elements[i].style.display = 'inline';
                      }
                      return (div.innerText || div.textContent).indexOf(str) > -1;
                  }
                  

                  这篇关于JavaScript window.find 绝对不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                • <tfoot id='WAFMi'></tfoot>
                    <bdo id='WAFMi'></bdo><ul id='WAFMi'></ul>

                    <small id='WAFMi'></small><noframes id='WAFMi'>

                    <i id='WAFMi'><tr id='WAFMi'><dt id='WAFMi'><q id='WAFMi'><span id='WAFMi'><b id='WAFMi'><form id='WAFMi'><ins id='WAFMi'></ins><ul id='WAFMi'></ul><sub id='WAFMi'></sub></form><legend id='WAFMi'></legend><bdo id='WAFMi'><pre id='WAFMi'><center id='WAFMi'></center></pre></bdo></b><th id='WAFMi'></th></span></q></dt></tr></i><div id='WAFMi'><tfoot id='WAFMi'></tfoot><dl id='WAFMi'><fieldset id='WAFMi'></fieldset></dl></div>
                      <tbody id='WAFMi'></tbody>

                          <legend id='WAFMi'><style id='WAFMi'><dir id='WAFMi'><q id='WAFMi'></q></dir></style></legend>