将“返回false”添加到点击事件侦听器的效果如何? [英] What's the effect of adding 'return false' to a click event listener?

查看:83
本文介绍了将“返回false”添加到点击事件侦听器的效果如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多时候我在HTML页面看到过这样的链接:

 < a href ='#'onclick = someFunc(3.1415926); return false;'>点击此处!< / a> 

return false 的效果在那里?



另外,我通常不会在按钮中看到它。



这是指定的任何位置吗?在w3.org的某些规范中?

解决方案

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



我不相信这是W3C规范。像这样的所有古老的JavaScript接口都被赋予了昵称DOM 0,并且大部分是未指定的。你可能会看到旧的Netscape 2文档。



实现这种效果的现代方法是调用 event.preventDefault(),这在 DOM 2 Events规范


Many times I've seen links like these in HTML pages:

<a href='#' onclick='someFunc(3.1415926); return false;'>Click here !</a>

What's the effect of the return false in there?

Also, I don't usually see that in buttons.

Is this specified anywhere? In some spec in w3.org?

解决方案

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.

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

这篇关于将“返回false”添加到点击事件侦听器的效果如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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