简体   繁体   English

关于preventDefault的jquery澄清

[英]jquery clarification regarding preventDefault

http://api.jquery.com/event.preventDefault/ http://api.jquery.com/event.preventDefault/

"If this method is called, the default action of the event will not be triggered." “如果调用此方法,则不会触发事件的默认操作。”

"default action" means redirecting people from a different page when they click a hyperlink, as far as i observed in sample code. “默认操作”表示在点击超链接时从其他页面重定向人员,就我在示例代码中所观察到的那样。

is there any other use for this function? 这个功能还有其他用途吗?

how does it interact with forms? 它如何与表格互动?

As you said, preventDefault prevents the default action of the event from being triggered. 正如您所说, preventDefault阻止触发事件的默认操作。 The default action of the event depends on the type of event, and the event target. 事件的默认操作取决于事件类型和事件目标。

For example, if preventDefault was used in a click event handler for a checkbox, the checkbox would not become checked upon click. 例如,如果在复选框的单击事件处理程序中使用了preventDefault ,则单击时不会选中该复选框。 If it was used in a keydown event handler for an text input, the pressed key would not be written to the input value. 如果在文本输入的keydown事件处理程序中使用它,则不会将按下的键写入输入值。 If it was used in a submit event handler for a form, it would prevent the form from submitting. 如果它在表单的submit事件处理程序中使用,则会阻止表单提交。

Basically, it stops any event from performing the action it would perform by default. 基本上,它会阻止任何事件执行默认情况下执行的操作。

preventDefault will prevent the default event occuring, so, for example, if we linked the following code to a form - preventDefault将阻止发生默认事件,例如,如果我们将以下代码链接到表单 -

$('#form').submit(function(e) {
  alert('Handler for .submit() called.');
  e.preventDefault();
});

The e.preventDefault() call will prevent the form being submitted as that is the default event that would normally occur on a form submit. e.preventDefault()调用将阻止提交表单,因为这是表单提交时通常会发生的默认事件。

It prevents the default action of the element, whatever that default action might be. 它会阻止元素的默认操作,无论该默认操作是什么。

In the case of forms and inputs, in response to a click or such, it would prevent checking a radio-button or checkbox, it would prevent the submission of the form (or whatever action was specified) when clicking the submit button. 在表单和输入的情况下,响应于点击等,它将阻止检查单选按钮或复选框,它将阻止单击submit按钮时submit表单(或指定的任何操作)。

preventDefault() prevents any default action of browser or so for the specified event. preventDefault()阻止浏览器对指定事件的任何默认操作。

There are many practical use of preventDefault() , I have summarized a few for you. preventDefault()有很多实际用途,我总结了一些适合你的。 You will also find the answer to your question about forms within this. 您还可以在此找到有关表单的问题的答案。

It prevents: 它可以防止:

  • Submitting form while clicking submit button 单击“提交”按钮提交表单
  • Submitting form while pressing enter 按Enter键提交表单
  • Sometimes it can also prevent scrolling 有时它也可以防止滚动
  • Keypress actions also. Keypress行动也。
  • Right click browser menu 右键单击浏览器菜单
  • Opening new tabs while clicking middle button on link 单击链接上的中间按钮时打开新选项卡
  • Opening links while clicking left button, as you mentioned, 如上所述,在单击左键的同时打开链接,

And there are many 还有很多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM