开发者

Scroll down at character number N in HTML

开发者 https://www.devze.com 2023-01-07 03:52 出处:网络
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 ma

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

精彩评论

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