Bry*_*yan 162 javascript jquery preventdefault
一旦我解雇了evt.preventDefault(),我怎么能再次恢复默认操作?
Gra*_*mas 84
根据@Prescott的评论,与之相反:
evt.preventDefault();
Run Code Online (Sandbox Code Playgroud)
可能:
基本上等同于'默认',因为我们不再阻止它.
否则,我倾向于向您指出另一条评论和答案提供的答案:
如何取消绑定调用event.preventDefault()的侦听器(使用jQuery)?
请注意,第二个已被一个示例解决方案接受,由redsquare给出(如果没有作为重复关闭,则在此处发布以获得直接解决方案):
$('form').submit( function(ev) {
ev.preventDefault();
//later you decide you want to submit
$(this).unbind('submit').submit()
});
Run Code Online (Sandbox Code Playgroud)
fox*_*gga 51
function(evt) {evt.preventDefault();}
Run Code Online (Sandbox Code Playgroud)
和它的对立面
function(evt) {return true;}
Run Code Online (Sandbox Code Playgroud)
干杯!
Bra*_*ood 15
要在继续clickjQuery中的事件链接之前处理命令:
例如: <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");
}
});
Run Code Online (Sandbox Code Playgroud)
或者干脆运行window.location = this.href;后preventDefault();
小智 8
event.preventDefault(); //or event.returnValue = false;
Run Code Online (Sandbox Code Playgroud)
及其相反(标准):
event.returnValue = true;
Run Code Online (Sandbox Code Playgroud)
来源:https : //developer.mozilla.org/en-US/docs/Web/API/Event/returnValue
小智 7
好 !它适用于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 !
});
Run Code Online (Sandbox Code Playgroud)
我建议采用以下模式:
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>
Run Code Online (Sandbox Code Playgroud)
...除非我错过了什么。
小智 6
我不得不在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();
});
});
Run Code Online (Sandbox Code Playgroud)
这里有一些有用的...
首先,我们将点击链接,运行一些代码,然后我们将执行默认操作。这可以使用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>
Run Code Online (Sandbox Code Playgroud)
我认为“相反”是模拟一个事件。你可以使用.createEvent()
按照 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");
}
}
Run Code Online (Sandbox Code Playgroud)
jQuery 可以.trigger()让您触发元素上的事件——有时很有用。
$('#foo').bind('click', function() {
alert($(this).text());
});
$('#foo').trigger('click');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
158731 次 |
| 最近记录: |