开发者

Scroll down at character number N in HTML

I can calculate an order of a character I would like to open a HTML page at.

Is it 开发者_C百科possible (with jQuery) to write a script that will scroll down to a position of nth character in HTML markup?

Let's say this is my HTML:

<p>Hello</p>
<p>Hello</p>
<p>Scroll here</p>
<p>Hello</p>

How would I scroll down to 28th character in that HTML (so

Scroll here

will be where the page will start)?


If it's also enough to scroll to the element, do it like

$(document.body).scrollTop($('p:nth-child(3)').offset().top);

but instead of the offset value you can also just set a value like

$(window).scrollTop(28);

Another way is to call scrollIntoView() like

$('p:nth-child(3)')[0].scrollIntoView();


JavaScript works on the DOM, and the DOM doesn't necessarily have a one-to-one relationship to the HTML, so any approach would need a lot of hacks.

Do you mean significant character, or including whitespace?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜