Javascript查找并滚动到文本 [英] Javascript find and scroll to text
问题描述
我需要一些Javascript,它会在html页面中找到一些文字,然后滚动到那一点。
I need a bit of Javascript that will find some text in the html page and then scroll to that point.
所以像你是自由民主党还是保守党的支持者,你对这笔交易感觉如何?将向下滚动到此bbc新闻页面的页面底部: http://news.bbc.co.uk/1/hi/uk_politics/election_2010/8676607.stm
So something like "Are you a Lib Dem or Tory supporter and how do you feel about the deal?" would scroll down to the bottom of the page for this bbc news page: http://news.bbc.co.uk/1/hi/uk_politics/election_2010/8676607.stm
我希望有内置功能找到文本和滚动。
Im hoping there is a built in function for both the find text and scroll.
推荐答案
试试这个。它适用于您提供的网站:
Try this. It works on the site you provided:
$(window).scrollTop($("*:contains('Are you a Lib Dem or Tory'):last").offset().top);
它找到包含页面上给定短语的最后一个最深的元素并滚动到它。
It finds last, deepest element that contains given phrase on the page and scrolls to it.
这篇关于Javascript查找并滚动到文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!