我正在观察 onClick 事件导致页面重新加载的行为。
$('#target').click(function(){
$('#target').hide();
})
因此显示对象,它在单击时隐藏,但随后页面重新加载并再次显示对象。我在一个在我之前建立的网站上工作,所以我并不完全了解它的所有部分。知道什么可能导致这种行为以及如何解决它吗?
最佳答案
您需要使用 event.preventDefault
来阻止默认事件行为:
$("#target").on("click", function (e) {
$(this).hide();
e.preventDefault();
});
使用 return false
也可以,但它 does more than you may intend .
关于javascript - JQuery 事件导致页面重新加载。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15889105/