Javascript evt.preventDefault() 的反面是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5651933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the opposite of evt.preventDefault();
提问by Bryan
Once I've fired an evt.preventDefault()
, how can I resume default actions again?
一旦我触发了一个evt.preventDefault()
,我怎样才能再次恢复默认操作?
回答by Grant Thomas
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.
本质上等同于'do default',因为我们不再阻止它。
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?
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()
});
回答by foxybagga
function(evt) {evt.preventDefault();}
and its opposite
和它的反面
function(evt) {return true;}
cheers!
干杯!
回答by Bradley Flood
To process a command before continue a link from a click
event in jQuery:
在从click
jQuery 中的事件继续链接之前处理命令:
Eg: <a href="http://google.com/" class="myevent">Click me</a>
例如: <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();
或者干脆运行window.location = this.href;
后preventDefault();
回答by Bobby Pearson
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();
});
});
回答by SNS Web
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 !
});
回答by user10625817
event.preventDefault(); //or event.returnValue = false;
and its opposite(standard) :
和它的对立面(标准):
event.returnValue = true;
source: https://developer.mozilla.org/en-US/docs/Web/API/Event/returnValue
来源:https: //developer.mozilla.org/en-US/docs/Web/API/Event/returnValue
回答by Partha
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 的功能本质上是调用 return 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()
。您只是不阻止该事件,默认情况下,如果您不阻止该事件,浏览器将允许该事件。
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。这意味着我们将阻止我们的表单提交使用event.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.
这确实是与此相反的唯一有效方法event.preventDefault()
- 您也可以尝试删除事件,这基本上可以实现相同的目的。
回答by Rafael Xavier
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.currentTargetTake a look. Here we'll gonna try to access Google on a new tab, but before we need to run some code.
首先,我们将点击链接,运行一些代码,然后我们将执行默认操作。这可以使用event.currentTarget来看看。在这里,我们将尝试在新选项卡上访问 Google,但在我们需要运行一些代码之前。
<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>
回答by karim79
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.
...除非我遗漏了什么。
回答by Gary Green
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 cancelled = !cb.dispatchEvent(evt);
if(cancelled) {
// A handler called preventDefault
alert("cancelled");
} else {
// None of the handlers called preventDefault
alert("not cancelled");
}
}
Ref: 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');