开发者

Referring to a specific place on page

开发者 https://www.devze.com 2022-12-27 07:23 出处:网络
I know that in Javascript document.location.href = \"#my_id\" tells the browser to display the same page starting from element with id=\"my_id\".

I know that in Javascript document.location.href = "#my_id" tells the browser to display the same page starting from element with id="my_id". In this case, the address that appears in the address bar is in the following format: my_page_address#my_id

Is this the only method to refer to a specific place on a page ? I'm looking for a method that will not show my_id in the address 开发者_JS百科bar.


Most browsers implement the scrollIntoView method (MDC, MSDN) on elements. That works on IE6 and up (at least), Firefox and other Gecko-based browsers, Chrome and other WebKit-based browsers, Opera, etc.

scrollIntoView example using an element retrieved by ID:

document.getElementById("my_id").scrollIntoView();

Of course, this requires that Javascript be enabled (I'm assuming this is okay because of the Javascript tag on the question :-) ).

You can also scroll to specific coordinates on the page using window.scrollTo.


Have you tried document.getElementbyId("my_id").scrollIntoView()?

0

精彩评论

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