一旦我触发了event . preventdefault(),我如何再次恢复默认操作?
当前回答
这里有一些有用的东西……
首先,我们将单击链接,运行一些代码,然后执行默认操作。这可以使用event实现。看看吧。在这里,我们将尝试在一个新选项卡上访问谷歌,但在我们需要运行一些代码之前。
<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>
其他回答
这段代码为我重新实例化事件后,我已经使用:
event.preventDefault(); to disable the event.
event.preventDefault = false;
好的!它适用于点击事件:
$("#submit").click(function(event){
event.preventDefault();
// -> block the click of the sumbit ... do what you want
// the html click submit work now !
$("#submit").unbind('click').click();
});
这是我用来设置的:
$("body").on('touchmove', function(e){
e.preventDefault();
});
要撤消它:
$("body").unbind("touchmove");
在jQuery中处理一个命令,然后从点击事件继续链接:
例:<a href="http://google.com/" class="myevent">点击我</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()之后;
为了执行异步调用,我不得不在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();
});
});
推荐文章
- Javascript和regex:分割字符串并保留分隔符
- 如何检查DST(日光节约时间)是否有效,如果是,偏移量?
- 如何打破_。在underscore.js中的每个函数
- 如何在jQuery中获得当前日期?
- 如何创建一个日期对象从字符串在javascript
- 输入触发器按钮单击
- 获取对象的属性名
- 如何检查用户是否可以回到浏览器历史
- 使用jQuery的“.val()”在表单中设置隐藏字段的值不工作
- 相当于字符串。jQuery格式
- jQuery模板引擎
- 如何在vue-cli项目中更改端口号
- Angular 2模板中的标签是什么意思?
- JavaScript .includes()方法的多个条件
- 窗口。亲近与自我。close不关闭Chrome中的窗口