Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery.on(“单击”)是否总是在<;之前运行;a>;火灾? $(“a”)。在(“单击”)上,函数(e){ doSomething(); }); ..._Jquery - Fatal编程技术网

jQuery.on(“单击”)是否总是在<;之前运行;a>;火灾? $(“a”)。在(“单击”)上,函数(e){ doSomething(); }); ...

jQuery.on(“单击”)是否总是在<;之前运行;a>;火灾? $(“a”)。在(“单击”)上,函数(e){ doSomething(); }); ...,jquery,Jquery,在每个浏览器中,doSomething()是否总是在“href”之前运行?是的,处理程序总是首先运行。例如,这允许您在必要时取消默认行为(导航到href url) $("a").on("click", function (e) { doSomething(); }); ... <a href="http://website.com">My Link</a> 是的。如果不希望href触发,可以调用e.preventDefault()和浏览器将不跟随链接。是的,但只

在每个浏览器中,
doSomething()
是否总是在“href”之前运行?

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

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

是的。如果不希望href触发,可以调用
e.preventDefault()和浏览器将不跟随链接。

是的,但只有在第一个
之前运行的部分等待着:

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

对如果有一个事件绑定到单击,那么它的返回值控制是否有后续导航。因此,要澄清的是,即使
e.preventDefault()
出现在处理程序的最末端,在大量代码之后,它仍然会取消href fire?如果在单击函数中,您可以说正在将跟踪信息推送到另一台服务器。使用Fiddler,我看到在链接被跟随之前没有任何东西被推动。使用OnClick似乎总是进行推送,然后跟随链接。使用on和click似乎总是跟随链接,而不会将数据推送到其他地方。
$("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`
});