Jquery - .click() 不滚动页面

发布于 10-16 06:17 字数 498 浏览 10 评论 0原文

我将在 jQuery 上创建一个如下所示的函数:

$(document).ready(function() {
    $('#header_rules').click(function() {
      // here i define myFunction()
    });
});

<a id="header_rules" href="#">RULES</a>

而不是

 <a id="header_rules" href="#" onClick="myFunction();return false">RULES</a>

But I don't know how to防止在有人单击链接后使用 jQuery 滚动到页面顶部。

事实上,我将完全摆脱原生 JS,这可能会产生一些麻烦(这就是我使用 jQuery 等 JS 框架的原因)。

有什么帮助吗?干杯

I'm about to make a function on jQuery like this:

$(document).ready(function() {
    $('#header_rules').click(function() {
      // here i define myFunction()
    });
});

<a id="header_rules" href="#">RULES</a>

instead of

 <a id="header_rules" href="#" onClick="myFunction();return false">RULES</a>

But I don't know how to prevent scrolling to the top of the page using jQuery after someone clicks the link.

In fact, I'll total get rid about native JS, which could yield some trouble (that's why I use a JS framework like jQuery).

Any help? Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹倦2024-10-23 06:17:13

调用 e.preventDefault();,其中 e 是点击处理程序的第一个参数:

$(document).ready(function() {
    $('#header_rules').click(function(e) {
        e.preventDefault();
        // here i define myFunction()
    });
});

<a id="header_rules" href="#">RULES</a>

Call e.preventDefault(); where e is the first argument of your click handler:

$(document).ready(function() {
    $('#header_rules').click(function(e) {
        e.preventDefault();
        // here i define myFunction()
    });
});

<a id="header_rules" href="#">RULES</a>
软甜啾2024-10-23 06:17:13

下面的代码也可以工作,但我认为它会停止该项目上的其他点击事件。

(document).ready(function() {
    $('#header_rules').click(function() {
      // here i define myFunction()
    return false;
    });
});

The below will also work, but it'll stop other click events on the item, I think.

(document).ready(function() {
    $('#header_rules').click(function() {
      // here i define myFunction()
    return false;
    });
});
永不分离2024-10-23 06:17:13

正如 ThiefMaster 所说,您还可以在函数中包含 return false; 而不是内联。

What ThiefMaster said, and you could also inclue return false; inside your function intead of inline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文