使用 window.find() 进行环绕搜索不起作用 [英] Wrap around search with window.find() doesn't work

查看:36
本文介绍了使用 window.find() 进行环绕搜索不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 window.config() 函数通过按钮触发搜索.但是,环绕布尔值设置为 true 但实际上不起作用...

I'm trying to use the window.config() function to trigger search with a button. However, the wrap around boolean is set on true but doesn't actually work...

语法:window.find(aString, aCaseSensitive, aBackwards, aWrapAround, aWholeWord, aSearchInFrames, aShowDialog);

我的代码:window.find(text, false, false, true, false, false, false);

你有什么想法吗?谢谢:)

Do you have some ideas ? Thank you :)

编辑:我终于在这个网站上找到了解决方案:http://www.javascripter.net/faq/searchin.htm

EDIT : I finally found the solution on this website : http://www.javascripter.net/faq/searchin.htm

正是我需要的:)

推荐答案

事实上,您的问题是您正在调用 .find() 方法container 而不是 window 元素,这就是它不起作用的原因,因为它是 window 对象的一部分,并且无法在 HTML 对象上访问.

In fact your problem is that you were calling .find() method on container instead of window element, that's why it isn't working because it's part of windowobject and is not accessible on HTML objects.

只需将您的代码更改为:

Just change your code to:

 if (window.find) {
  strFound = window.find(str);
  if (!strFound) {
   strFound = window.find(str, 0, 1);
   while (window.find(str, 0, 1)) continue;
  }
 }

演示:

这是一个工作演示以及您的更新的 CodePen.

This is a working demo along with your updated CodePen.

$('#search-btn').on('click', () => {
  findString('lorem')
});

function findString(str) {
  let strFound;
  if (window.find) {
    strFound = window.find(str);
    if (!strFound) {
      strFound = window.find(str, 0, 1);
      while (window.find(str, 0, 1)) continue;
    }
  }
  if (!strFound) alert("String '" + str + "' not found!")
  return;
}

#container {
  width:800px;
  height:200px;
  overflow:scroll
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/Window/find -->
lorem
<div id="log"></div>
<div id="container">
  Le passage de Lorem Ipsum standard, utilisé depuis 1500 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
  ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  Section 1.10.32 du "De Finibus Bonorum et Malorum" de Ciceron (45 av. J.-C.) "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto
  beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit
  amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea
  commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" Traduction de H. Rackham (1914) "But I must explain to you how
  all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects,
  dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain
  of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage
  from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"</div>

<button id="search-btn" type="button">Search for Lorem</button>

这篇关于使用 window.find() 进行环绕搜索不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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