开发者

What are the cases when `return false` does not do the same thing as `e.preventDefault()`? [duplicate]

开发者 https://www.devze.com 2023-03-13 11:22 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: JavaScript: event开发者_如何学JAVA.preventDefault() vs return false
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

JavaScript: event开发者_如何学JAVA.preventDefault() vs return false

What are the cases when return false does not do the same thing as e.preventDefault() ?

(Note I'm not talking about jQuery events.. but JavaScript events in general)

The only situation I can think of is a click event on a hyperlink (if you return false in the handler you will still be redirected to the website)

Note that if its not a click event but a mousedown event on the hyperlink return false works same as e.preventDefault().


I believe that return false is equivalent to e.preventDefault(); and e.stopPropagation(); at the same time. However, I recommend that you use the functions and the return false for future and past browser compatibility:

$("#something").click(function(e){
    alert("Something was clicked");
    e.preventDefault();
    return false;
});

Ad@m

0

精彩评论

暂无评论...
验证码 换一张
取 消