The meaning of return false in the click event

I accidentally saw the word 'return false;' appearing in the callback function of a jquery click event. I was confused, so I googled it.

$('xxx').on('click', function() {
    xxx
    xxx
    ...
    return false;
});

Found a clear explanation in stackoverflow.

When 'return false' appears in a function , it means that event handling prevents the default event behavior and stops the event from bubbling up through the dom.

Equivalent to:

$('xxx').on('click', function(event) {
    ...
    ...
    event.preventDefault();//阻止事件的默认行为
    event.stopPropagation();//阻止该dom节点往上冒泡
})

for example:

If xxx is an a tag, its default behavior is to jump, and when return false; is set , it will not jump.



Author: JohnsonChe
Link: https://www.jianshu.com/p/89a8fafd4b35
Source : Jianshu The copyright of Jianshu
belongs to the author. For any form of reprint, please contact the author for authorization and indicate the source.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324188448&siteId=291194637