jquery 关于 PreventDefault 的澄清

发布于 2024-12-04 16:22:06 字数 248 浏览 0 评论 0原文

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三寸金莲 2024-12-11 16:22:06

正如您所说,preventDefault 阻止触发事件的默认操作。事件的默认操作取决于事件类型和事件目标。

例如,如果在复选框的单击事件处理程序中使用 preventDefault,则该复选框在单击时不会被选中。如果在文本输入的 keydown 事件处理程序中使用它,则按下的键不会写入输入值。如果在表单的 submit 事件处理程序中使用它,则会阻止表单提交。

基本上,它会阻止任何事件执行默认情况下执行的操作。

As you said, preventDefault prevents the default action of the event from being triggered. 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. If it was used in a keydown event handler for an text input, the pressed key would not be written to the input value. If it was used in a submit event handler for a form, it would prevent the form from submitting.

Basically, it stops any event from performing the action it would perform by default.

旧时模样 2024-12-11 16:22:06

preventDefault 将阻止默认事件发生,因此,例如,如果我们将以下代码链接到表单 -

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

e.preventDefault() 调用将阻止提交表单因为这是表单提交时通常发生的默认事件。

preventDefault will prevent the default event occuring, so, for example, if we linked the following code to a form -

$('#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.

无悔心 2024-12-11 16:22:06

它阻止元素的默认操作,无论默认操作是什么。

在表单和输入的情况下,响应单击等,它将阻止检查单选按钮或复选框,它将阻止在单击 submit< 时提交表单(或指定的任何操作) /代码> 按钮。

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.

雪若未夕 2024-12-11 16:22:06

preventDefault() 防止浏览器等对指定事件执行任何默认操作。

preventDefault()的实际用途有很多,我给大家总结了几个。您还可以在其中找到有关表单的问题的答案。

它可以防止:

  • 单击“提交”按钮时
  • 提交表单 按 Enter 键时提交表单
  • 有时,它还可以防止滚动
  • 按键操作。
  • 右键单击浏览器菜单
  • 单击链接上的中间按钮时打开新选项卡
  • 单击左侧按钮时打开链接,正如您所提到的,

并且有很多

preventDefault() prevents any default action of browser or so for the specified event.

There are many practical use of preventDefault(), I have summarized a few for you. 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
  • Sometimes it can also prevent scrolling
  • Keypress actions also.
  • 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文