简体   繁体   中英

My website alway scroll up when i click link?

I have a link

<a href="#">Text</a>

when i click this link my page alway scroll up to the top. How do i manage it that when i clik this link my page not scroll up to the top.

Javascript? or something

thank you

you can add some javascript to deny the default behavior.

function myClickHandler(e) {

    // your code here
    // ...

    // new code
    if(e.preventDefault){ //firefox,chrome
        e.preventDefault();
    }
    else { // ie
        return false;
    }
}

if you provide some more detail/example code, we can give you a more specific answer.

Not sure what you are trying to do, but maybe you are thinking of:

<a href="JavaScript:void(0);" >Text</a>

that'll do nothing.

You might want to post an example of a link that does this. My guess is that it's because you don't have an href set for the link or you ended the link href with a "#someId"

It's not that it's scrolling to the top of the page, it's refreshing the page.

An example of a top link:

<a href="#header">Some Link</a>

<a href="#">Somewhere</a> <!-- will refresh and you end up at the top -->

EDIT Ah... Now that you've provided the link... it's the Hash # that's the problem.

To avoid that from happening ( I'm guessing you want to do some Javascript on the link and you're trying to get it to do something.. ) then you need return false; in your javascript. This will return false from the link and won't follow it.

<body>
    <h1 id="top">First Headline</h1>

    <!-- your document here-->
    <a href="#top">go to Top</a>

</body>

With Javascript you could add some smoothness like slowly scroll up. HTML Links

It is because you have only the hash # as "URL". It makes the browser jump to the top of the page (normally it would jump to the element with the corresponding ID if you specify any).

But what is the purpose of such a link if you don't use it?

The [relative] URL # is treated by browsers as the top of the page. Either change the link's href attribute to refer to another resource, or add a click event handler that prevents the default action. Better yet, if you intend it to be a button that triggers a click event, replace the <a> tag with a <button> which is more semantically correct anyway.

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