简体   繁体   中英

Referring to a specific place on page

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 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 .

您是否尝试过document.getElementbyId("my_id").scrollIntoView()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM