JavaScript的window.find不能正常工作 [英] JavaScript window.find doesn't work absolutely
问题描述
<!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 更新#2:纯JavaScript解决方案 p> When I try to pass text which spreads throughout a few block elements the window.find method dosent work: HTML: JavaScript: Or: This happens when the How can I fix that? As option: 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 this call I created this example to test mentioned behavior. UPDATE #1: jQuery I've made deeper research and found that usage of jQuery UPDATE #2: pure JavaScript solution
这篇关于JavaScript的window.find不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!:选择器比我以前的函数更好,比window.find更好,但是可能会更昂贵(不确定,需要测试)。
$ p $ 函数containsStr $(str){
返回$('body:contai ns('+ str +')')。length> 0;
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;
}
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
</head>
<body>
<p>search me</p><b> I could be the answer</b>
</body>
</html>
window.find("meI could be");
str = "me";
str+= "\n";
str+="I could be t";
window.find(str);
<p>
element is present between the search term.function containsStr(str) {
return document.body.innerText.indexOf(str) > -1;
}
p {
display: inline;
}
window.find("me I could be")
returns true
for your HTML example.
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.: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;
}
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;
}