一旦我解雇了evt.preventDefault()
,我该如何重新恢复默认操作?
根据@Prescott的评论,与之相反:
evt.preventDefault();
可能:
基本上等同于'默认',因为我们不再阻止它。
否则,我倾向于向您指出另一条评论和答案提供的答案:
How to unbind a listener that is calling event.preventDefault() (using jQuery)?
How to reenable event.preventDefault?
请注意,第二个已被一个示例解决方案接受,由redsquare给出(如果没有作为重复关闭,则在此处发布以获得直接解决方案):
$('form').submit( function(ev) {
ev.preventDefault();
//later you decide you want to submit
$(this).unbind('submit').submit()
});
这是我以前设置它:
$("body").on('touchmove', function(e){
e.preventDefault();
});
并撤消它:
$("body").unbind("touchmove");
我认为“相反”是模拟一个事件。你会用qazxsw poi
按照Mozilla的例子:
.createEvent()
参考: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");
}
}
jQuery有document.createEvent所以你可以触发元素上的事件 - 有时很有用。
.trigger()
jquery $('#foo').bind('click', function() {
alert($(this).text());
});
$('#foo').trigger('click');
可能是另一个解决方案。当涉及到on()的使用时。
jquery on()只是当前绑定事件的方式(而不是bind())。 off()是取消绑定这些。当您使用命名空间时,您可以添加和删除多个不同的事件。
namespaces
现在使用命名空间,你可以添加多个这些事件,并且能够根据你的需要删除它们。虽然提交可能不是最好的例子,这可能会在点击或按键或其他任何事情上派上用场。
你可以在“preventDefault”方法之后使用它
//这里evt.target返回默认事件(例如:defult url等)
$( 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");
}
});
//这里我们保存默认事件..
var defaultEvent=evt.target;
您始终可以将此附加到脚本中的某些单击事件:
if("true")
{
//activate default event..
location.href(defaultEvent);
}
用法的例子是:
location.href = this.href;
这些解决方案都没有帮助我,我这样做是为了解决我的情况。
jQuery('a').click(function(e) {
location.href = this.href;
});
功能<a onclick="return clickEvent(event);" href="/contact-us">
,
clickEvent()
我使用了以下代码。这对我来说可以。
function clickEvent(event) {
event.preventDefault();
// do your thing here
// remove the onclick event trigger and continue with the event
event.target.parentElement.onclick = null;
event.target.parentElement.click();
}
在我使用之后,这段代码可以让我重新实例化事件:
$('a').bind('click', function(e) {
e.stopPropagation();
});
function(evt) {evt.preventDefault();}
和它的对立面
function(evt) {return true;}
干杯!
要在继续jQuery中的click
事件的链接之前处理命令:
例如:<a href="http://google.com/" class="myevent">Click me</a>
使用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");
}
});
或者只是在window.location = this.href;
之后运行preventDefault();
event.preventDefault(); //or event.returnValue = false;
和它的对立面(标准):
event.returnValue = true;
来源:https://developer.mozilla.org/en-US/docs/Web/API/Event/returnValue
我不得不在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();
});
});
好 !它适用于click事件:
$("#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 !
});
我会建议以下模式:
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>
......除非我遗漏了什么。
event.preventDefault()
没有相反的方法可以理解为什么你首先要调查event.preventDefault()
在你调用它时所做的事情。
在引擎盖下,preventDefault的功能本质上是调用return false,它会停止任何进一步的执行。如果您熟悉Javascript的旧方法,那么曾经使用return false来取消事件,例如使用return true(在jQuery甚至可用之前)取消表单提交和按钮之类的事件。
正如你可能已经基于上面的简单解释所做的那样:与event.preventDefault()
相反的是什么。您只是不阻止该事件,默认情况下,如果您没有阻止该事件,浏览器将允许该事件。
请参阅下面的解释:
;(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);
在上面的代码中,您将注意到我们正在检查allowSubmit是否为false。这意味着我们将阻止我们的表单使用event.preventDefault
提交,然后我们将执行一些验证逻辑,如果我们满意,请将allowSubmit设置为true。
这实际上是与event.preventDefault()
相反的唯一有效方法 - 您也可以尝试删除事件,这基本上会实现相同的事情。
这是有用的东西......
首先,我们将点击链接,运行一些代码,然后我们将执行默认操作。这可以使用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>