jquery 关于 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如您所说,
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 akeydown
event handler for an text input, the pressed key would not be written to the input value. If it was used in asubmit
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.
preventDefault
将阻止默认事件发生,因此,例如,如果我们将以下代码链接到表单 -e.preventDefault()
调用将阻止提交表单因为这是表单提交时通常发生的默认事件。preventDefault
will prevent the default event occuring, so, for example, if we linked the following code to a form -The
e.preventDefault()
call will prevent the form being submitted as that is the default event that would normally occur on a form submit.它阻止元素的默认操作,无论默认操作是什么。
在表单和输入的情况下,响应单击等,它将阻止检查单选按钮或复选框,它将阻止在单击
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.preventDefault()
防止浏览器等对指定事件执行任何默认操作。preventDefault()
的实际用途有很多,我给大家总结了几个。您还可以在其中找到有关表单的问题的答案。它可以防止:
并且有很多
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:
And there are many