e.preventDefault阻止点击 [英] e.preventDefault prevent click
问题描述
我有以下代码来测试e.preventDefault()的工作原理。
我认为它会阻止点击事件的发生。
I have the following code to just test how the e.preventDefault() works. I would think it would stop the click event from happening.
$(document).ready(function( )
{
$("button").click(function(e)
{
e.preventDefault();
alert('button clicked');
});
});
我有一个按钮。 e.preventDefault()的目的是什么?
从我读到的内容中,它将阻止在这种情况下是点击的操作。
I have a button. What is the purpose of e.preventDefault(). From what I read, it will prevent the action which in this case is the click.
请注意,在上面的示例中,警告消息仍然显示。 e.preventDefault()如何用于按钮单击。我知道如果它是超链接,它会阻止超链接转到目标。
Note that in the above example, the alert message still show. How does e.preventDefault() work for a button click. I know if it was a hyperlink, it would prevent the hyperlink from going to it target.
推荐答案
preventDefault是为了防止默认,浏览器动作。因此,如果您要输入以下内容:
preventDefault is to prevent default, browser action. So if you will put something like:
$("a").click(function(e){e.preventDefault();})
点击链接将无能为力。或者在提交按钮 - 表单将不会被提交
click on links will do nothing. Or on submit button - form won't be submitted
这篇关于e.preventDefault阻止点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!