从 jQuery 单击事件返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11184276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:18:32  来源:igfitidea点击:

return false from jQuery click event

jqueryreturnjquery-events

提问by Alan2

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?

有人可以解释这是做什么以及是否需要?

回答by nnnnnn

When you return falsefrom 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:

当您false从事件处理程序返回时,它会阻止该事件的默认操作并停止该事件通过 DOM 冒泡。也就是说,它相当于这样做:

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

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

如果'.dialogLink'是一个<a>元素,那么它在点击时的默认操作是导航到href. 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.

至于您的情况是否需要它,我猜答案是肯定的,因为您想显示一个对话框来响应点击而不是导航。如果您放置点击处理程序的元素在点击时没有默认操作(例如,通常当您点击一个 div 时不会发生任何事情),那么您不需要返回 false,因为没有什么可以取消。

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

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

Further reading:

进一步阅读:

回答by Praveen Kumar Purushothaman

The return value of an event handler determines whether or not the default browser behaviour should take place as well. In the case of clicking on links, this would be following the link, but the difference is most noticeable in form submit handlers, where you can cancel a form submission if the user has made a mistake entering the information.

事件处理程序的返回值决定了是否也应该发生默认浏览器行为。在点击链接的情况下,这将跟随链接,但在表单提交处理程序中差异最为明显,如果用户输入信息有误,您可以取消表单提交。

I don't believe there is a W3C specification for this. All the ancient JavaScript interfaces like this have been given the nickname "DOM 0", and are mostly unspecified. You may have some luck reading old Netscape 2 documentation.

我不相信有 W3C 规范。像这样的所有古老的 JavaScript 接口都被赋予了“DOM 0”的绰号,而且大多是未指定的。您可能有幸阅读旧的 Netscape 2 文档。

The modern way of achieving this effect is to call event.preventDefault(), and this is specified in the DOM 2 Events specification.

实现这种效果的现代方法是调用event.preventDefault(),这在DOM 2 事件规范中指定。

So the right way would be:

所以正确的方法是:

$('.dialogLink')
   .click(function (e) {
       dialog(this);
       e.preventDefault();
       e.stopPropagation(); // Stop bubbling up
});

回答by kavindra chand

The return value of an event handler determines whether or not the default browser behaviour should take place as well. In the case of clicking on links, this would be following the link, but the difference is most noticeable in form submit handlers, where you can cancel a form submission if the user has made a mistake entering the information.

事件处理程序的返回值决定了是否也应该发生默认浏览器行为。在点击链接的情况下,这将跟随链接,但在表单提交处理程序中差异最为明显,如果用户输入信息有误,您可以取消表单提交。