JavaScript的window.find不能正常工作 [英] JavaScript window.find doesn't work absolutely

查看:148
本文介绍了JavaScript的window.find不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试传递遍布几个块元素的文本时,window.find方法开始工作:

p>

 <!DOCTYPE html> 
< html>
< head>
< meta charset = utf-8 />
< / head>
< body>
< p>搜寻我< / p>< b>我可以回答< / b>
< / body>
< / html>

JavaScript

  window.find(meI could be); 

或者:

  str =me; 
str + =\\\
;
str + =我可能是t;

window.find(str);

< p> 元素是存在于搜索词语之间。



我该如何解决这个问题? 解决方案

>

作为选项:

$ $ p $ 函数containsStr(str){
return document.body.innerText.indexOf str)> -1;
}

刚刚测试过,它和window.find一样。或者window.find和我的功能一样。
无论如何,这似乎取决于元素的style.display属性。
E.g.当我设置

  p {
display:inline;

$ / code>

这个调用 window.find(我可以)为您的HTML示例返回 true


$ b

我创建了 这个例子 来测试上述行为。

可以在内存中创建 div 元素,获取document.body.innerHTML并将检索值设置为div的 innerHTML ,然后更改 style.dysplay inline,用于 div 类似于我在代码示例中所做的,然后执行搜索。



更新#1:jQuery



我做了更深入的研究,发现使用jQuery :选择器比我以前的函数更好,比window.find更好,但是可能会更昂贵(不确定,需要测试)。

$ p $ 函数containsStr $(str){
返回$('body:contai ns('+ str +')')。length> 0;

更新#2:纯JavaScript解决方案 p>

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


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

Or:

str = "me";
str+= "\n";
str+="I could be t";

window.find(str);

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

How can I fix that?

解决方案

As option:

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

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

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

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.

UPDATE #1: jQuery

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

UPDATE #2: pure JavaScript solution

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不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆