$("a").on("click", function (e) {
doSomething();
});
...
<a href="http://website.com">My Link</a>
doSomething()
会在每个浏览器中都在"href"之前运行吗?
$("a").on("click", function (e) {
doSomething();
});
...
<a href="http://website.com">My Link</a>
doSomething()
会在每个浏览器中都在"href"之前运行吗?
是的,你的处理程序将始终首先运行。这就允许你在必要时取消默认行为(导航到href url)。
$("a").on("click", function (e) {
e.preventDefault(); // --> if this handle didn't run first, this wouldn't work
doSomething();
});
e.preventDefault()
在处理程序的最后,经过了很多代码,它仍然会取消href的触发吗? - D.Tatee.preventDefault();
,浏览器就不会跟随该链接。是的,但仅限于第一个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`
});