HTML JS Click the button to scroll to navigate to the page to specify the location (DIV) of the method code

Source: Internet
Author: User

One: Scroll through the HTML anchor point to the page specified location (DIV):
If we want to click on the implementation of the jump is an HTML anchor point, that is, click on a tag hyperlink to achieve a jump, you can put the href attribute of a tag directly to jump to the specified position of the Div, the code implementation ideas are as follows:

<a class= "banner" href= "/schoolfair/registration#nav" >

<a href= "#abc" > click to jump </a>
<div id= "ABC" > is going to jump here </div>

Second: Use JS to scroll to the page specified location (DIV) by clicking the button buttons:
If we want to click on the implementation of the jump to the place is a button, because the button can not add href, so we have to use the JS jump code to implement, the specific code example is as follows:
<script>
function Ontopclick () {
Window.location.hash = "#abc";
}
</script>
<input type= "button" name= "Submit" value= "commit" onclick= "Javascript:ontopclick ();"/>
<div id= "abc" > Jump to the location </div>
The above code, click the Submit button, will scroll to navigate to the same page id= "abc" Div. This JS click Jump page Code implementation of the principle is: the page each element to give a unique ID, click the Submit button to trigger the JS Click event, JS through the ID scrolling jump to navigate to the element, Window.location.hash = "#abc" refers to the location of the current page id= "ABC" The Div.

Window.location.href: Redirect, followed by the full URL address, and similar to the Window.location.hash,

Here's a comparison between Window.location.href and Window.locaiton.hash.

(1) window.location.href

Get and use the complete URL, such as window.location.href= "www.baidu.com" means redirect, page jump

to the new page. You can also get the full href of a tag by window.location.href, such as <a href= "#book" > If you use href, then

You can get the full link (URL)

(2) Window.location.hash

Get the anchor chain connection. Compared to the href, through Window.location.hash does not jump to the new link, only in the current link inside

Change the anchor chain. And if there is <a href= "#book" > through Window.location.hash not get the complete link (URL), just get #book.

HTML JS Click the button to scroll to navigate to the page to specify the location (DIV) of the method code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.