如何在onclick方法中防止默认?我有一个方法,在这个方法中我还传递了一个自定义值
<a href="#" onclick="callmymethod(24)">Call</a>
function callmymethod(myVal){
//doing custom things with myVal
//here I want to prevent default
}
发布于 2013-03-15 18:08:30
在我看来,答案是错误的!当您只返回false时,他要求使用event.preventDefault();
;它还会调用event.preventDefault();
和event.stopPropagation();
!
你可以这样解决这个问题:
<a href="#" onclick="callmymethod(event, 24)">Call</a>
function callmymethod(e, myVal){
//doing custom things with myVal
//here I want to prevent default
e = e || window.event;
e.preventDefault();
}
发布于 2013-01-24 17:54:44
试试这个(但是如果你没有有效的graceful degradation的href
值,请在这种情况下使用按钮)
<a href="#" onclick="callmymethod(24); return false;">Call</a>
发布于 2015-02-19 23:29:38
您可以捕获事件,然后使用preventDefault()阻止它--使用纯Javascript
document.getElementById("xyz").addEventListener('click', function(event){
event.preventDefault();
console.log(this.getAttribute("href"));
/* Do some other things*/
});
https://stackoverflow.com/questions/7056669
复制相似问题