What's the effect of adding 'return false' to a click event listener?

前端 未结 16 1765
南旧
南旧 2020-11-21 22:59

Many times I\'ve seen links like these in HTML pages:

Click here !
16条回答
  •  独厮守ぢ
    2020-11-21 23:33

    The return value of an event handler determines whether or not the default browser behaviour should take place as well. In the case of clicking on links, this would be following the link, but the difference is most noticeable in form submit handlers, where you can cancel a form submission if the user has made a mistake entering the information.

    I don't believe there is a W3C specification for this. All the ancient JavaScript interfaces like this have been given the nickname "DOM 0", and are mostly unspecified. You may have some luck reading old Netscape 2 documentation.

    The modern way of achieving this effect is to call event.preventDefault(), and this is specified in the DOM 2 Events specification.

提交回复
热议问题