简体   繁体   English

当我单击链接时,我的网站总是向上滚动吗?

[英]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? Javascript? or something 或者其他的东西

thank you 谢谢

you can add some javascript to deny the default behavior. 您可以添加一些JavaScript来拒绝默认行为。

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" 我的猜测是,这是因为您没有为链接设置href,或者您以“ #someId”结束了链接href

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; 为了避免这种情况的发生(我猜您想在链接上做一些Javascript,并且试图使它做点什么。。)然后您需要return false; in your javascript. 在您的JavaScript中。 This will return false from the link and won't follow it. 这将从链接返回false,并且不会关注它。

<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. 使用Javascript,您可以添加一些平滑度,例如慢慢向上滚动。 HTML Links HTML链接

It is because you have only the hash # as "URL". 这是因为您只有井号#作为“ 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). 它使浏览器跳到页面顶部(通常,如果您指定了ID,则它将跳到具有相应ID的元素)。

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. 浏览器将[相对] URL #视为页面顶部。 Either change the link's href attribute to refer to another resource, or add a click event handler that prevents the default action. 更改链接的href属性以引用其他资源,或者添加阻止默认操作的click事件处理程序。 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. 更好的是,如果您希望将其用作触发click事件的按钮, click<button>替换<a>标记,无论如何在语义上更正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我点击图片时如何让我的网站滚动到顶部 - How to get my website to scroll to the top when I click on an image 为什么当我在页面顶部滚动并单击附加表格的链接时,它会向下滚动 - Why when i scroll top my page and click link which append a form it scroll me down 如何获得锚链接,使其在向下滚动时像在其上一样平滑地向上滚动? - How can I get my anchor link to scroll smoothly back up top like it does when it goes down? 滚动时,为什么我的网站在Chrome中显示滞后? - Why does my website show lag in Chrome when I scroll? 单击按钮时如何重定向到我的网站? - How can I redirect to my website when I click a button? 网站内容溢出时,Cant不能向上滚动吗? - When website content overflows, Cant scroll up? 滚动网站-当滚动部分通过时,我希望导航div更改颜色 - Scroll Website - I want my navigation div change color when scroll pass a section 如何在网站加载时自动点击链接 - How to automatically click a link when the website loads 如何在我的网站上添加一个元标记,以便在我的iPhone Web视图中,当该人持有链接时不会显示弹出窗口? - How do I add a meta tag to my website so that in my iPhone web view, the pop up doesn't show when the person holds a link? 单击分页页面时向上滚动 - Scroll up when you click on the pagination page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM