jQuery .on("click")总是在<a> href触发之前运行吗?

D.T*_*ate 19 jquery

$("a").on("click", function (e) {
    doSomething();
});
...
<a href="http://website.com">My Link</a>
Run Code Online (Sandbox Code Playgroud)

doSomething()在"HREF"前始终运行,在每个浏览器?

Cla*_*edi 21

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

$("a").on("click", function (e) {
   e.preventDefault(); // --> if this handle didn't run first, this wouldn't work
   doSomething();
});
Run Code Online (Sandbox Code Playgroud)