查找文本并使用 javascript 滚动到其中
我有一个包含数百行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 this 之类的函数来查找文本并突出显示它。然后,您可以像这样滚动到突出显示的元素:
或者您可以像这样简单地转到 id
但是
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:
or you can simply go the id like this
However
window.scrollTo
is more flexible because you can set the element to wherever you want on the page.