Jquery:单击链接后阻止窗口滚动

Joh*_*hny 5 jquery

我有2个链接,隐藏主页上的用户不同的信息.如果单击一个链接,则新信息将显示在屏幕上.问题是,当我点击任何链接时,窗口将被带回页面顶部.我想防止这种行为,因为它很烦人.

这是链接的代码

<div align="right" id="ajaxIncomingMail"><a href="#" class="incomingMail">Incoming Mail</a></div><div id="ajaxOutgoingMail"><h5><a href="#" class="outgoingMail">Outgoing Mail</a></h5></div>
Run Code Online (Sandbox Code Playgroud)

这是jquery的代码:

        $(function(){
      $("a.incomingMail").click(function(e){
            e.preventDefault();
            $("#ajaxMailShowContentOutbox").hide();
            $("#ajaxMailShowContentInbox").fadeIn("slow");
        });

        $("a.outgoingMail").click(function(e){
            e.preventDefault();
            $("#ajaxMailShowContentInbox").hide();
            $("#ajaxMailShowContentOutbox").fadeIn("slow");
        });
        return false;
    });
Run Code Online (Sandbox Code Playgroud)

在这里,我使用的是preventDefault(),它仍然无法正常工作!?我也尝试过返回false,但这也不起作用.我不知道它是否重要,但所提供的信息是从数据库中提取的.点击链接后如何才能使滚动停止?

gri*_*egs 11

return false;应该在实际的jQuery点击事件中,而不是在它之外吗?

编辑

    $(function(){
  $("a.incomingMail").click(function(e){
        e.preventDefault();
        $("#ajaxMailShowContentOutbox").hide();
        $("#ajaxMailShowContentInbox").fadeIn("slow");
        return false;
    });

    $("a.outgoingMail").click(function(e){
        e.preventDefault();
        $("#ajaxMailShowContentInbox").hide();
        $("#ajaxMailShowContentOutbox").fadeIn("slow");
        return false;
    });

});
Run Code Online (Sandbox Code Playgroud)


anI*_*Mer 5

您可能在 a 标签中有 href='#' ,将其更改为 href='###' 并且它应该停止滚动。