return false
works cross browser, but only for event handlers assigned the "DOM0" way, such as
document.getElementById("link").onclick = function() {
alert("Clicked!");
return false;
};
For event handlers assigned the DOM Level 2 way via addEventListener()
, you have to use preventDefault()
:
document.getElementById("link").addEventListener("click", function(evt) {
alert("Clicked!");
evt.preventDefault();
}, false);
For event listeners attached via attachEvent()
in IE, either return false
or window.event.returnValue = false
will do:
document.getElementById("link").attachEvent("onclick", function() {
alert("Clicked!");
return false;
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…