查找文本并使用 javascript 滚动到其中

发布于 2024-09-15 19:27:30 字数 247 浏览 1 评论 0原文

我有一个包含数百行的 div。 我想创建一个函数,允许我查找文本并滚动到其中:

function findAndScroll(text)

因此,我在输入文本中输入所需的文本,单击“Go”按钮将触发“findAndScroll”函数,然后滚动到该文本。

在编码之前,是否有现有的 jQuery 插件或 JavaScript 库 那可以做到这一点吗?

谢谢您,

问候。

I have a div that contains hundreds of lines.
I want to create a function that allows me to find a text and scroll into it:

function findAndScroll(text)

So, I enter the wanted text in an input text, I click on the "Go" button that will trigger the "findAndScroll" function then I get scrolled to that text.

Before coding, is there an existant jQuery plugin or a javascript library
that can do this?

Thank you,

Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2024-09-22 19:27:30

您可以使用 this 之类的函数来查找文本并突出显示它。然后,您可以像这样滚动到突出显示的元素:

var offset = $("#id_of_highlighted_element").offset().top;
window.scrollTo(0,offset);

或者您可以像这样简单地转到 id

window.location = "#id_of_highlighted_element";

但是 window.scrollTo 更灵活,因为您可以将元素设置到页面上的任何位置。

You can use a function like this to find the text and highlight it. You can then scroll to the highlighted element like this:

var offset = $("#id_of_highlighted_element").offset().top;
window.scrollTo(0,offset);

or you can simply go the id like this

window.location = "#id_of_highlighted_element";

However window.scrollTo is more flexible because you can set the element to wherever you want on the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文