一旦我触发了event . preventdefault(),我如何再次恢复默认操作?
当前回答
这里没有一个解决方案对我有帮助,我这样做是为了解决我的情况。
<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();
}
其他回答
好的!它适用于点击事件:
$("#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();
});
在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()之后;
这里有一些有用的东西……
首先,我们将单击链接,运行一些代码,然后执行默认操作。这可以使用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>
你可以在你的脚本中使用这个附加到一些点击事件:
location.href = this.href;
用法示例如下:
jQuery('a').click(function(e) {
location.href = this.href;
});
event.preventDefault(); //or event.returnValue = false;
及其反义词(标准):
event.returnValue = true;
来源: https://developer.mozilla.org/en-US/docs/Web/API/Event/returnValue