简体   繁体   English

如何阻止调用javascript函数的#link从跳转到页面顶部

[英]how to stop # links that call javascript functions from jumping to top of page

How do you I stop my links like this: 你如何阻止我这样的链接:

<a href="#" onClick="submitComment()">

From jumping to the top of the page after the click? 从点击后跳到页面顶部?

Many times you'll see people use the onclick attribute, and simply return false at the end of it. 很多时候你会看到人们使用onclick属性,并且只是在结尾处return false While this does work reliably, it's a bit ugly and may make your code-base difficult to manage. 虽然这确实可行,但它有点难看,可能会使您的代码库难以管理。

<a href="#" onClick="submitComment(); return false;">

Seperate your HTML from your JavaScript 从JavaScript中分离HTML

It's far better for you, and your project if you separate your markup from your scripting. 如果你将标记与脚本分开,那对你和你的项目来说会好得多。

<a id="submit" href="enableScripts.html">Post Comment</a>

With the above HTML, we can find this element and wire up a handler for when the user clicks on the element. 使用上面的HTML,我们可以找到这个元素,并在用户点击元素时连接一个处理程序。 We can do all of this from an external .js file so that our HTML remains nice and clean, free from any scripting: 我们可以从外部.js文件中完成所有这些操作,以便我们的HTML保持良好和干净,不受任何脚本编写:

if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", setup, false);
} else if (document.attachEvent) {
    document.attachEvent("onreadystatechange", setup);
} else {
    document.onload = setup;
}

function setup () {
    var submit, submitComment;
    submit = document.getElementById("submit");
    submitComment = function (e) {
        e = e || window.event;
        e.preventDefault();
        alert("You clicked the link!");
    };
    if (submit.addEventListener) {
        submit.addEventListener("click", submitComment, false);
    } else if (submit.attachEvent) {
        submit.attachEvent("onclick", submitComment);
    } else {
        submit["onclick"] = submitComment;
    }
}

There's a lot going on in the above code, but let's run over it from 30,000 feet. 上面的代码中有很多内容,但让我们在30,000英尺的范围内运行它。 We start by figuring out how to best setup our code when the browser loads the page up. 我们首先弄清楚当浏览器加载页面时如何最好地设置我们的代码。 Ideally we'd like to do this when the DOM is ready. 理想情况下,我们想在DOM准备就绪时这样做。

After a few conditional checks we manage to instruct the browser to run our function after the DOM is prepared (this way our anchor element exists for us to interact with its behavior). 在几次条件检查之后,我们设法指示浏览器在准备好DOM之后运行我们的函数(这样我们的锚元素就可以让我们与它的行为进行交互)。

Our setup function gets a reference to this anchor, creates a function that we'll run when the anchor is clicked, and then finds a way to attach that function call to the click event of the anchor - losing your mind yet? 我们的setup函数获取对这个锚点的引用,创建一个我们在单击锚点时运行的函数,然后找到一种方法将该函数调用附加到锚点的click事件 - 失去理智吗? This is the madness JavaScript developers have had to deal with for some time now. 这是JavaScript开发人员一段时间以来不得不处理的疯狂。

With jQuery, this is much easier 使用jQuery,这更容易

Perhaps you've heard of jQuery, and wondered why it is so popular. 也许你已经听说过jQuery,并想知道为什么它如此受欢迎。 Let's solve the same problem, but this time with jQuery rather than raw vanilla JavaScript. 让我们解决同样的问题,但这次使用jQuery而不是原始的vanilla JavaScript。 Assuming the following markup: 假设以下标记:

<a id="submit" href="enableScripts.html">Post Comment</a>

The only JavaScript we need (thanks to jQuery) is this: 我们需要的唯一JavaScript(感谢jQuery)是这样的:

$(function(){
    $("#submit").on("click", function(event){
        event.preventDefault();
        submitComment();
    });
});

That's it - that is all it takes. 就是这样 - 这就是全部。 jQuery handles all of the tests to determine, given your browser, what the best way is to do this or that . jQuery的处理所有的测试来确定的,因为你的浏览器,最好的办法是做这样那样的东西。 It takes all of the complicated stuff and moves it out of the way so that you are free to be creative. 它需要所有复杂的东西并将其移开,以便您可以自由发挥创意。

Add a return false. 添加返回false。 I believe that without that the page will reload. 我相信没有它,页面将重新加载。

<a href="#" onClick="submitComment(); return false;">

Just return false onClick of Hyperlink 只需返回false on on Hyperlink

Its will not scroll page up ie it will be still where it is 它不会向上滚动页面,即它仍然是它的位置

Unless you need the anchor to actual go somewhere, you don't need to use an "href=" reference at all. 除非你需要锚到实际去某处,否则根本不需要使用“href =”引用。

Try just using <a id="stay-put">Submit Comment</a> 尝试使用<a id="stay-put">Submit Comment</a>

Then your javascript would look like this: 然后你的JavaScript看起来像这样:

$("#stay-put").click(function(){
    submitComment();
});

Try using: 尝试使用:

<a href="#a" onclick="myFunction()">Javascript is sweet!</a>

This will anchor the page in the place it is at. 这会将页面锚定在它所在的位置。 I had this same issue and this was a simple and good fix for me. 我有同样的问题,这对我来说是一个简单而好的修复。

An other simple way is 另一种简单的方法是

<a href="javascript: void(0)" class="action-class">Link</a>

Then you could have a separate code with onClick event to the class "action-class" with whatever framework you like or plain JavaScript. 然后,您可以使用onClick事件将单独的代码添加到类“action-class”中,使用您喜欢的任何框架或纯JavaScript。

您可以使用此替代语法:

<a href="javascript:submitComment()">

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

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