I wondered if event.preventDefault()
and return false
were the same.
I have done some tests, and it seems that
If the event handler is added using old model, for example
elem.onclick = function(){
return false;
};
Then, return false
prevents default action, like event.preventDefault()
.
If the event handler is added using addEventListener
, for example
elem.addEventListener(
'click',
function(e){
return false;
},
false
);
Then, return false
doesn't prevent the default action.
Do all browsers behave like this?
Are there more differences between event.preventDefault()
and return false
?
Where I can find some documentation (I couldn't in MDN) about return false
behaving like event.preventDefault()
in some cases?
My question is only about plain javascript, not jQuery, so please don't mark it as a duplicate of event.preventDefault() vs. return false, even if both questions have almost the same title.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…