22
4

3 回答 3

23

是的,您的处理程序将始终首先运行。例如,这就是允许您在必要时取消默认行为(导航到 href url)的原因

$("a").on("click", function (e) {
   e.preventDefault(); // --> if this handle didn't run first, this wouldn't work
   doSomething();
});
于 2013-04-25T16:19:06.773 回答
3

是的,它确实。如果您不希望触发 href,您可以调用e.preventDefault();并且浏览器不会跟随该链接。

于 2013-04-25T16:19:17.437 回答
1

是的,但只有在第一个之前运行的部分await

$("a").on("click", async function(e) {
  example(); // this will always run

  await Promise.resolve();

  example3(); // this may not run, as it is after the first `await`, and it is after nagivation
  e.preventDefault(); // this does not work, as it is after the first `await`
});
于 2020-03-04T15:24:49.877 回答