• <tfoot id='6248F'></tfoot>

      • <bdo id='6248F'></bdo><ul id='6248F'></ul>

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

        <small id='6248F'></small><noframes id='6248F'>

        <legend id='6248F'><style id='6248F'><dir id='6248F'><q id='6248F'></q></dir></style></legend>
      1. JavaScript window.find() 不选择搜索词

        时间:2023-10-02

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

                <tbody id='dBwya'></tbody>
              <tfoot id='dBwya'></tfoot>
              <legend id='dBwya'><style id='dBwya'><dir id='dBwya'><q id='dBwya'></q></dir></style></legend>
                <bdo id='dBwya'></bdo><ul id='dBwya'></ul>
                1. <small id='dBwya'></small><noframes id='dBwya'>

                  本文介绍了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> 元素出现在搜索词之间时会发生这种情况.
                  最终结果应该是页面中文本的 GUI 选择,我不想搜索它是否存在.

                  This happens when the <p> element is present between the search term.
                  The end result should be a GUI selection on the text in the page, I do not want to search if it exists.

                  我想知道如何在 Firefox(至少)和 Internet Explorer 中实现这一点.
                  注意:我无法更改 dom(例如更改为内联显示).

                  I would like to know how to achieve this in Firefox(at least) and internet explorer.
                  Note: I can't change the dom (e.g. change to display inline).


                  这是我在@Alexey Lebedev 发表评论后尝试的,但它也找到了脚本(标记 [...] 文本):


                  Here is something I tried after @Alexey Lebedev's comment, but it also finds the script (tag [...] text):

                  我可以让它更简单吗?(更好)?

                  Can I make it more simple? (better)?

                  function nativeTreeWalker(startNode) {
                      var walker = document.createTreeWalker(
                          startNode, 
                          NodeFilter.SHOW_TEXT, 
                          null, 
                          false
                      );
                      var node;
                      var textNodesV = [];
                      var textNodes = [];
                      node = walker.nextNode();
                  
                      while(node ) {
                        if(node.nodeValue.trim()){
                          textNodes.push(node);
                          textNodesV.push(node.nodeValue);
                          //console.log(node.nodeValue);
                        }
                          node = walker.nextNode();
                      }
                    return [textNodes,textNodesV];
                  }
                  
                  var result = nativeTreeWalker(document.body);
                  var textNodes = result[0];
                  var textNodesV = result[1];
                  
                  var param = " Praragraph.Test 3 Praragr";
                  paramArr = param.split(/(?=[S])(?!s)(?=[W])(?=[S])/g);
                  //Fix split PARAM
                  for(i=0;i<paramArr.length-1;i++){
                    paramArr[i]= paramArr[i]+paramArr[i+1].charAt(0);
                    paramArr[i+1] = paramArr[i+1].substring(1,paramArr[i+1].length);
                  }
                  //Fix last element PARAM
                  if(paramArr[paramArr.length-1] === ""){
                    paramArr.splice(paramArr.length-1,1);
                  }
                  //console.log(paramArr);
                  var startNode,startOffset,sFound=false,
                      endNode,endOffset;
                  for(i=0;i<paramArr.length;i++){
                    for(j=0;j<textNodesV.length;j++){
                      //Fully Equal
                      var pos = textNodesV[j].indexOf(paramArr[i]);
                      if(pos != -1){
                        if(!sFound){
                          startNode = textNodes[j];
                          startOffset = pos;
                          sFound=true;
                        }else{
                          endNode = textNodes[j];
                          endOffset = pos+paramArr[i].length;
                          break;
                        }
                      }
                    }
                  }
                  console.log(startNode);
                  console.log(startOffset);
                  console.log(endNode);
                  console.log(endOffset);
                  
                  var range = document.createRange();
                  range.setStart(startNode,startOffset);
                  range.setEnd(endNode,endOffset);
                  var sel = window.getSelection();
                  sel.removeAllRanges();
                  sel.addRange(range);
                  

                  注意:没有 jQuery(只有 Raw JS).

                  Note: No jQuery (only Raw JS).

                  推荐答案

                  JS Bin 演示:http://jsbin.com/aqiciv/1/

                  如果你想让它在 IE 中工作 <9 您需要添加特定于 MS 的选择代码(噩梦),或使用 Rangy.js(相当繁重).

                  If you want this to work in IE < 9 you'll need to add MS-specific selection code (nightmare), or use Rangy.js (pretty heavy).

                  function visibleTextNodes() {
                      var walker = document.createTreeWalker(
                          document.body,
                          NodeFilter.SHOW_ALL,
                          function(node) {
                              if (node.nodeType == 3) {
                                  return NodeFilter.FILTER_ACCEPT;
                              } else if (node.offsetWidth && node.offsetHeight && node.style.visibility != 'hidden') {
                                  return NodeFilter.FILTER_SKIP;
                              } else {
                                  return NodeFilter.FILTER_REJECT;
                              }
                          },
                          false
                      );
                  
                      for (var nodes = []; walker.nextNode();) {
                          nodes.push(walker.currentNode);
                      }
                      return nodes;
                  }
                  
                  // Find the first match, select and scroll to it.
                  // Case- and whitespace- insensitive.
                  // For better scrolling to selection see https://gist.github.com/3744577
                  function highlight(needle) {
                      needle = needle.replace(/s/g, '').toLowerCase();
                  
                      var textNodes = visibleTextNodes();
                  
                      for (var i = 0, texts = []; i < textNodes.length; i++) {
                          texts.push(textNodes[i].nodeValue.replace(/s/g, '').toLowerCase());
                      }
                  
                      var matchStart = texts.join('').indexOf(needle);
                      if (matchStart < 0) {
                          return false;
                      }
                  
                      var nodeAndOffsetAtPosition = function(position) {
                          for (var i = 0, consumed = 0; consumed + texts[i].length < position; i++) {
                              consumed += texts[i].length;
                          }
                          var whitespacePrefix = textNodes[i].nodeValue.match(/^s*/)[0];
                          return [textNodes[i], position - consumed + whitespacePrefix.length];
                      };
                  
                      var range = document.createRange();
                      range.setStart.apply(range, nodeAndOffsetAtPosition(matchStart));
                      range.setEnd.apply(  range, nodeAndOffsetAtPosition(matchStart + needle.length));
                      window.getSelection().removeAllRanges();
                      window.getSelection().addRange(range);
                      range.startContainer.parentNode.scrollIntoView();
                  }
                  
                  highlight('hello world');
                  

                  这篇关于JavaScript window.find() 不选择搜索词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Find 查找 HTML 片段的 Ajax 响应 下一篇:JavaScript window.find 绝对不能正常工作

                  相关文章

                  1. <small id='zBD6m'></small><noframes id='zBD6m'>

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