关于 javascript:e.preventDefault() 的位置是否重要 | 珊瑚贝

does location of e.preventDefault() matter


我在我的 jquery 函数中使用 preventDefault() 来防止提交按钮以正常方式提交。 preventDefault() 的位置是否重要。它必须先出现还是可以是函数中的最后一件事?


不,没关系:
https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

这与返回 false 不同,因为当你返回时,函数中的所有其他内容都将被放弃。

如上面给出的 MDC 链接中所说:

Calling preventDefault during any
stage of event flow cancels the event,
meaning that any default action
normally taken by the implementation
as a result of the event will not
occur.

  • li,看起来你是对的,我已经返回 false,感谢您澄清这一点,我删除了我的答案


没关系,但我喜欢把它放在第一位。当代码位于顶部时,快速扫描代码即可明确预期行为。


来源:https://www.codenong.com/4476973/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?