如果将以下事件处理程序绑定到我的form
:
form.addEventListener('submit', function(evt) {
evt.preventDefault();
console.log('submitted');
this.submit();
});
当客户端提交表单时,将执行事件处理程序并执行the form is submitted without the event handler being executed again.
但是,在将click
个处理程序附加到anchor
个元素时,相同的逻辑不适用:
a.addEventListener('click', function(evt) {
evt.preventDefault();
console.log('clicked');
this.click();
});
事件处理程序执行两次,并且从不遵循链接.
我有两个问题:
- 为什么它在
submit
和click
之间的工作方式有所不同? - 如果每次调用事件处理程序时都会执行它,为什么锚的
click
处理程序只执行两次,而不是无限期执行?