Why does a `click` event get triggered on my <button> when I press Enter on it?

前端 未结 4 801
梦毁少年i
梦毁少年i 2021-01-04 06:39

I\'m only adding a click event handler on my

4条回答
  •  不知归路
    2021-01-04 07:27

    This is largely because lots of authors have historically written code using click events while forgetting to account for users who don't click (whether because they prefer to use a keyboard to navigate, have a disability which makes it hard to use a pointing device, or whatever other reason).

    The behaviour is documented in the HTML specification:

    Certain elements in HTML have an activation behavior, which means that the user can activate them. This triggers a sequence of events dependent on the activation mechanism, and normally culminating in a click event, as described below.

    For accessibility, the keyboard’s Enter and Space keys are often used to trigger an element’s activation behavior.

    It then goes on to explain the steps in detail.

提交回复
热议问题