javascript - My website alway scroll up when i click link? - Stack Overflow

I have a link <a href="#">Text<a>when i click this link my page alway scroll up

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

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

Share Improve this question edited Jul 19, 2010 at 19:56 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Jul 19, 2010 at 19:51 GiffaryGiffary 3,14813 gold badges54 silver badges77 bronze badges 1
  • 2 I guess the question is why do you have a link with only a hash in it? It's behaving correctly, so the question is what are you trying to acplish with this link. – Nelfo Commented Jul 19, 2010 at 20:02
Add a ment  | 

6 Answers 6

Reset to default 5

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.

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.

<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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745111082a4611855.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信