从jQuery click事件返回false [英] return false from jQuery click event

查看:105
本文介绍了从jQuery click事件返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的点击事件设置如下:

I have click events set up like this:

$('.dialogLink')
    .click(function () {
        dialog(this);
        return false;
    });

所有人都有一个返回假"

The all have a "return false"

有人可以解释它的作用以及是否需要它吗?

Can someone explain what this does and if it's needed?

推荐答案

从事件处理程序返回false时,它将阻止该事件的默认操作并停止该事件通过DOM冒泡.也就是说,这等效于执行此操作:

When you return false from an event handler it prevents the default action for that event and stops the event bubbling up through the DOM. That is, it is the equivalent of doing this:

$('.dialogLink')
   .click(function (event) {
       dialog(this);
       event.preventDefault();
       event.stopPropagation();
});

如果'.dialogLink'<a>元素,则其默认的单击动作是导航到href.从点击处理程序中返回false可以防止这种情况.

If '.dialogLink' is an <a> element then its default action on click is to navigate to the href. Returning false from the click handler prevents that.

至于您是否需要它,我想答案是肯定的,因为您想显示一个对话框来响应单击而不是导航.如果您放置了点击处理程序的元素没有默认的点击操作(例如,当您点击div时通常没有任何反应),则无需返回false,因为没有要取消的操作.

As for whether it is needed in your case, I would guess the answer is yes because you want to display a dialog in response to the click rather than navigating. If the element you've put the click handler on does not have a default action on click (e.g., normally nothing happens when you click a div) then you needn't return false because there's nothing to cancel.

如果您想对点击做出响应,但继续默认导航,则不要返回false.

If you want to do something in response to the click but let the default navigation continue then do not return false.

进一步阅读:

  • event.preventDefault()
  • event.stopPropagation()

这篇关于从jQuery click事件返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆