Once I've fired an evt.preventDefault()
, how can I resume default actions again?
一旦我触发了一个evt.preventDefault(),如何再次恢复默认操作?
15 个解决方案
#1
62
As per commented by @Prescott, the opposite of:
正如@Prescott评论的那样:
evt.preventDefault();
Could be:
可能是:
Essentially equating to 'do default', since we're no longer preventing it.
本质上等同于“做违约”,因为我们不再阻止它。
Otherwise I'm inclined to point you to the answers provided by another comments and answers:
否则,我倾向于给你们指出其他评论和答案所提供的答案:
How to unbind a listener that is calling event.preventDefault() (using jQuery)?
如何解绑定调用event.preventDefault()的侦听器(使用jQuery)?
How to reenable event.preventDefault?
如何使再能event.preventDefault吗?
Note that the second one has been accepted with an example solution, given by redsquare (posted here for a direct solution in case this isn't closed as duplicate):
注意,第二种方法已经被一个示例解决方案所接受,redsquare给出了这个示例解决方案(在这里发布的是一个直接解决方案,以防止它作为副本关闭):
$('form').submit( function(ev) {
ev.preventDefault();
//later you decide you want to submit
$(this).unbind('submit').submit()
});
#2
45
function(evt) {evt.preventDefault();}
and its opposite
和它相反
function(evt) {return true;}
cheers!
干杯!
#3
15
To process a command before continue a link from a click
event in jQuery:
在继续jQuery单击事件的链接之前,要处理命令:
Eg: <a href="http://google.com/" class="myevent">Click me</a>
Prevent and follow through with jQuery:
使用jQuery进行预防和跟踪:
$('a.myevent').click(function(event) {
event.preventDefault();
// Do my commands
if( myEventThingFirst() )
{
// then redirect to original location
window.location = this.href;
}
else
{
alert("Couldn't do my thing first");
}
});
Or simply run window.location = this.href;
after the preventDefault();
或者只是运行窗口。位置= this.href;后preventDefault();
#4
5
I had to delay a form submission in jQuery in order to execute an asynchronous call. Here's the simplified code...
为了执行异步调用,我必须在jQuery中延迟表单提交。这是简化代码…
$("$theform").submit(function(e) {
e.preventDefault();
var $this = $(this);
$.ajax('/path/to/script.php',
{
type: "POST",
data: { value: $("#input_control").val() }
}).done(function(response) {
$this.unbind('submit').submit();
});
});
#5
4
I would suggest the following pattern:
我建议采取以下模式:
document.getElementById("foo").onsubmit = function(e) {
if (document.getElementById("test").value == "test") {
return true;
} else {
e.preventDefault();
}
}
<form id="foo">
<input id="test"/>
<input type="submit"/>
</form>
...unless I'm missing something.
…除非我丢失的东西。
http://jsfiddle.net/DdvcX/
#6
4
OK ! it works for the click event :
好的!它适用于点击事件:
$("#submit").click(function(e){
e.preventDefault();
-> block the click of the sumbit ... do what you want
$("#submit").unbind('click').click(); // the html click submit work now !
});
#7
2
I supose the "opposite" would be to simulate an event. You could use .createEvent()
我认为“相反”将是模拟一个事件。您可以使用.createEvent()
Following Mozilla's example:
Mozilla的例子:
function simulateClick() {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var cb = document.getElementById("checkbox");
var canceled = !cb.dispatchEvent(evt);
if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}
}
Ref: document.createEvent
裁判:document.createEvent
jQuery has .trigger()
so you can trigger events on elements -- sometimes useful.
jQuery有.trigger(),因此可以在元素上触发事件——有时很有用。
$('#foo').bind('click', function() {
alert($(this).text());
});
$('#foo').trigger('click');
#8
1
jquery on() could be another solution to this. escpacially when it comes to the use of namespaces.
jquery on()可能是另一种解决方案。当涉及到名称空间的使用时。
jquery on() is just the current way of binding events ( instead of bind() ). off() is to unbind these. and when you use a namespace, you can add and remove multiple different events.
jquery on()只是绑定事件的当前方式(而不是bind()))。off()就是解开这些东西。当您使用名称空间时,您可以添加和删除多个不同的事件。
$( selector ).on("submit.my-namespace", function( event ) {
//prevent the event
event.preventDefault();
//cache the selector
var $this = $(this);
if ( my_condition_is_true ) {
//when 'my_condition_is_true' is met, the binding is removed and the event is triggered again.
$this.off("submit.my-namespace").trigger("submit");
}
});
now with the use of namespace, you could add multiple of these events and are able to remove those, depending on your needs.. while submit might not be the best example, this might come in handy on a click or keypress or whatever..
现在有了名称空间的使用,您可以添加这些事件中的多个,并可以根据您的需要删除它们。虽然提交可能不是最好的例子,但在单击或按下键时,它可能会派上用场。
#9
1
this code worked for me to re-instantiate the event after i had used :
这段代码让我在使用后重新实例化事件:
event.preventDefault(); to disable the event.
event.preventDefault = false;
#10
0
you can use this after "preventDefault" method
您可以在“preventDefault”方法之后使用这个方法
//Here evt.target return default event (eg : defult url etc)
/ /这里evt。目标返回默认事件(例如:defult url等)
var defaultEvent=evt.target;
//Here we save default event ..
//这里我们保存默认事件。
if("true")
{
//activate default event..
location.href(defaultEvent);
}
#11
0
You can always use this attached to some click event in your script:
你可以把这个附在你的脚本中的点击事件:
location.href = this.href;
example of usage is:
使用的例子:
jQuery('a').click(function(e) {
location.href = this.href;
});
#12
0
This is what I used to set it:
这是我用来设置的
$("body").on('touchmove', function(e){
e.preventDefault();
});
And to undo it:
和撤销:
$("body").unbind("touchmove");
#13
0
There is no opposite method of event.preventDefault()
to understand why you first have to look into what event.preventDefault()
does when you call it.
没有与event.preventDefault()相反的方法来理解为什么您必须首先研究event.preventDefault()在调用它时的作用。
Underneath the hood, the functionality for preventDefault is essentially calling a return false which halts any further execution. If you’re familiar with the old ways of Javascript, it was once in fashion to use return false for canceling events on things like form submits and buttons using return true (before jQuery was even around).
在引擎盖下面,preventDefault的功能实质上是调用一个返回false,从而停止任何进一步的执行。如果您熟悉Javascript的老方法,那么曾经流行使用return false来取消表单提交和使用return true(在jQuery出现之前)的按钮等事件。
As you probably might have already worked out based on the simple explanation above: the opposite of event.preventDefault()
is nothing. You just don’t prevent the event, by default the browser will allow the event if you are not preventing it.
正如您可能已经基于上面的简单解释得出的:event.preventDefault()的反义词是nothing。你只是不阻止事件,默认情况下浏览器会允许事件发生如果你不阻止它。
See below for an explanation:
请看下面的解释:
;(function($, window, document, undefined)) {
$(function() {
// By default deny the submit
var allowSubmit = false;
$("#someform").on("submit", function(event) {
if (!allowSubmit) {
event.preventDefault();
// Your code logic in here (maybe form validation or something)
// Then you set allowSubmit to true so this code is bypassed
allowSubmit = true;
}
});
});
})(jQuery, window, document);
In the code above you will notice we are checking if allowSubmit is false. This means we will prevent our form from submitting using event.preventDefault
and then we will do some validation logic and if we are happy, set allowSubmit to true.
在上面的代码中,您将注意到我们正在检查allowSubmit是否为false。这意味着我们将阻止我们的表单提交使用事件。preventDefault,然后我们将执行一些验证逻辑,如果我们喜欢,将allowSubmit设置为true。
This is really the only effective method of doing the opposite of event.preventDefault()
– you can also try removing events as well which essentially would achieve the same thing.
这实际上是唯一有效的方法来做与事件相反的事情。preventdefault()——您也可以尝试删除事件,这基本上可以实现相同的事情。
#14
0
Here's something useful...
这里有一些有用的东西…
First of all we'll click on the link , run some code, and than we'll perform default action. This will be possible using event.currentTarget Take a look. Here we'll gonna try to access Google on a new tab, but before we need to run some code.
首先,我们点击链接,运行一些代码,然后我们将执行默认操作。这将是可能的使用事件。currentTarget看一看。在这里,我们将尝试在一个新的选项卡*问谷歌,但是在我们需要运行一些代码之前。
<a href="https://www.google.com.br" target="_blank" id="link">Google</a>
<script type="text/javascript">
$(document).ready(function() {
$("#link").click(function(e) {
// Prevent default action
e.preventDefault();
// Here you'll put your code, what you want to execute before default action
alert(123);
// Prevent infinite loop
$(this).unbind('click');
// Execute default action
e.currentTarget.click();
});
});
</script>
#15
-1
I have used the following code. It works fine for me.
我使用了以下代码。对我来说很好。
$('a').bind('click', function(e) {
e.stopPropagation();
});
#1
62
As per commented by @Prescott, the opposite of:
正如@Prescott评论的那样:
evt.preventDefault();
Could be:
可能是:
Essentially equating to 'do default', since we're no longer preventing it.
本质上等同于“做违约”,因为我们不再阻止它。
Otherwise I'm inclined to point you to the answers provided by another comments and answers:
否则,我倾向于给你们指出其他评论和答案所提供的答案:
How to unbind a listener that is calling event.preventDefault() (using jQuery)?
如何解绑定调用event.preventDefault()的侦听器(使用jQuery)?
How to reenable event.preventDefault?
如何使再能event.preventDefault吗?
Note that the second one has been accepted with an example solution, given by redsquare (posted here for a direct solution in case this isn't closed as duplicate):
注意,第二种方法已经被一个示例解决方案所接受,redsquare给出了这个示例解决方案(在这里发布的是一个直接解决方案,以防止它作为副本关闭):
$('form').submit( function(ev) {
ev.preventDefault();
//later you decide you want to submit
$(this).unbind('submit').submit()
});
#2
45
function(evt) {evt.preventDefault();}
and its opposite
和它相反
function(evt) {return true;}
cheers!
干杯!
#3
15
To process a command before continue a link from a click
event in jQuery:
在继续jQuery单击事件的链接之前,要处理命令:
Eg: <a href="http://google.com/" class="myevent">Click me</a>
Prevent and follow through with jQuery:
使用jQuery进行预防和跟踪:
$('a.myevent').click(function(event) {
event.preventDefault();
// Do my commands
if( myEventThingFirst() )
{
// then redirect to original location
window.location = this.href;
}
else
{
alert("Couldn't do my thing first");
}
});
Or simply run window.location = this.href;
after the preventDefault();
或者只是运行窗口。位置= this.href;后preventDefault();
#4
5
I had to delay a form submission in jQuery in order to execute an asynchronous call. Here's the simplified code...
为了执行异步调用,我必须在jQuery中延迟表单提交。这是简化代码…
$("$theform").submit(function(e) {
e.preventDefault();
var $this = $(this);
$.ajax('/path/to/script.php',
{
type: "POST",
data: { value: $("#input_control").val() }
}).done(function(response) {
$this.unbind('submit').submit();
});
});
#5
4
I would suggest the following pattern:
我建议采取以下模式:
document.getElementById("foo").onsubmit = function(e) {
if (document.getElementById("test").value == "test") {
return true;
} else {
e.preventDefault();
}
}
<form id="foo">
<input id="test"/>
<input type="submit"/>
</form>
...unless I'm missing something.
…除非我丢失的东西。
http://jsfiddle.net/DdvcX/
#6
4
OK ! it works for the click event :
好的!它适用于点击事件:
$("#submit").click(function(e){
e.preventDefault();
-> block the click of the sumbit ... do what you want
$("#submit").unbind('click').click(); // the html click submit work now !
});
#7
2
I supose the "opposite" would be to simulate an event. You could use .createEvent()
我认为“相反”将是模拟一个事件。您可以使用.createEvent()
Following Mozilla's example:
Mozilla的例子:
function simulateClick() {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var cb = document.getElementById("checkbox");
var canceled = !cb.dispatchEvent(evt);
if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}
}
Ref: document.createEvent
裁判:document.createEvent
jQuery has .trigger()
so you can trigger events on elements -- sometimes useful.
jQuery有.trigger(),因此可以在元素上触发事件——有时很有用。
$('#foo').bind('click', function() {
alert($(this).text());
});
$('#foo').trigger('click');
#8
1
jquery on() could be another solution to this. escpacially when it comes to the use of namespaces.
jquery on()可能是另一种解决方案。当涉及到名称空间的使用时。
jquery on() is just the current way of binding events ( instead of bind() ). off() is to unbind these. and when you use a namespace, you can add and remove multiple different events.
jquery on()只是绑定事件的当前方式(而不是bind()))。off()就是解开这些东西。当您使用名称空间时,您可以添加和删除多个不同的事件。
$( selector ).on("submit.my-namespace", function( event ) {
//prevent the event
event.preventDefault();
//cache the selector
var $this = $(this);
if ( my_condition_is_true ) {
//when 'my_condition_is_true' is met, the binding is removed and the event is triggered again.
$this.off("submit.my-namespace").trigger("submit");
}
});
now with the use of namespace, you could add multiple of these events and are able to remove those, depending on your needs.. while submit might not be the best example, this might come in handy on a click or keypress or whatever..
现在有了名称空间的使用,您可以添加这些事件中的多个,并可以根据您的需要删除它们。虽然提交可能不是最好的例子,但在单击或按下键时,它可能会派上用场。
#9
1
this code worked for me to re-instantiate the event after i had used :
这段代码让我在使用后重新实例化事件:
event.preventDefault(); to disable the event.
event.preventDefault = false;
#10
0
you can use this after "preventDefault" method
您可以在“preventDefault”方法之后使用这个方法
//Here evt.target return default event (eg : defult url etc)
/ /这里evt。目标返回默认事件(例如:defult url等)
var defaultEvent=evt.target;
//Here we save default event ..
//这里我们保存默认事件。
if("true")
{
//activate default event..
location.href(defaultEvent);
}
#11
0
You can always use this attached to some click event in your script:
你可以把这个附在你的脚本中的点击事件:
location.href = this.href;
example of usage is:
使用的例子:
jQuery('a').click(function(e) {
location.href = this.href;
});
#12
0
This is what I used to set it:
这是我用来设置的
$("body").on('touchmove', function(e){
e.preventDefault();
});
And to undo it:
和撤销:
$("body").unbind("touchmove");
#13
0
There is no opposite method of event.preventDefault()
to understand why you first have to look into what event.preventDefault()
does when you call it.
没有与event.preventDefault()相反的方法来理解为什么您必须首先研究event.preventDefault()在调用它时的作用。
Underneath the hood, the functionality for preventDefault is essentially calling a return false which halts any further execution. If you’re familiar with the old ways of Javascript, it was once in fashion to use return false for canceling events on things like form submits and buttons using return true (before jQuery was even around).
在引擎盖下面,preventDefault的功能实质上是调用一个返回false,从而停止任何进一步的执行。如果您熟悉Javascript的老方法,那么曾经流行使用return false来取消表单提交和使用return true(在jQuery出现之前)的按钮等事件。
As you probably might have already worked out based on the simple explanation above: the opposite of event.preventDefault()
is nothing. You just don’t prevent the event, by default the browser will allow the event if you are not preventing it.
正如您可能已经基于上面的简单解释得出的:event.preventDefault()的反义词是nothing。你只是不阻止事件,默认情况下浏览器会允许事件发生如果你不阻止它。
See below for an explanation:
请看下面的解释:
;(function($, window, document, undefined)) {
$(function() {
// By default deny the submit
var allowSubmit = false;
$("#someform").on("submit", function(event) {
if (!allowSubmit) {
event.preventDefault();
// Your code logic in here (maybe form validation or something)
// Then you set allowSubmit to true so this code is bypassed
allowSubmit = true;
}
});
});
})(jQuery, window, document);
In the code above you will notice we are checking if allowSubmit is false. This means we will prevent our form from submitting using event.preventDefault
and then we will do some validation logic and if we are happy, set allowSubmit to true.
在上面的代码中,您将注意到我们正在检查allowSubmit是否为false。这意味着我们将阻止我们的表单提交使用事件。preventDefault,然后我们将执行一些验证逻辑,如果我们喜欢,将allowSubmit设置为true。
This is really the only effective method of doing the opposite of event.preventDefault()
– you can also try removing events as well which essentially would achieve the same thing.
这实际上是唯一有效的方法来做与事件相反的事情。preventdefault()——您也可以尝试删除事件,这基本上可以实现相同的事情。
#14
0
Here's something useful...
这里有一些有用的东西…
First of all we'll click on the link , run some code, and than we'll perform default action. This will be possible using event.currentTarget Take a look. Here we'll gonna try to access Google on a new tab, but before we need to run some code.
首先,我们点击链接,运行一些代码,然后我们将执行默认操作。这将是可能的使用事件。currentTarget看一看。在这里,我们将尝试在一个新的选项卡*问谷歌,但是在我们需要运行一些代码之前。
<a href="https://www.google.com.br" target="_blank" id="link">Google</a>
<script type="text/javascript">
$(document).ready(function() {
$("#link").click(function(e) {
// Prevent default action
e.preventDefault();
// Here you'll put your code, what you want to execute before default action
alert(123);
// Prevent infinite loop
$(this).unbind('click');
// Execute default action
e.currentTarget.click();
});
});
</script>
#15
-1
I have used the following code. It works fine for me.
我使用了以下代码。对我来说很好。
$('a').bind('click', function(e) {
e.stopPropagation();
});