Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
Why does a browser apply CSS styles first before deciding what onclick event handler to call?

I have a single div on a page. It has an onclick event handler. When the user clicks on that div I expect that the event handler will be called. But there is this CSS rule as well: when that div has a focus, it will be moved 50 pixels to the right (away from the mouse pointer). As a result of this the onclick event handler is not called, even though the user clearly clicked on that div at the beginning. It's as if the browser first applies CSS and only after that decides, what did the user clicked on.

I made a simple demo here: https://jsbin.com/yalazam/edit?html,css,console,output Click on the yellow square (that is the div) in the fourth column and see the console in the third column. Only a message "focus square" will appear, but not a "click square".

Does this behavior makes any sense? Is there any case when it is useful? Or should I just accept it as a weird behavior of the browser?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The relevant point here consists in the definition of what a click event is:

An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element. If the button is pressed on one element and the pointer is moved outside the element before the button is released, the event is fired on the most specific ancestor element that contained both elements.

This means that in your example, no click event is fired on the div you're targeting, because although the mouse press happens inside that div, the release happens after it has moved (unless the user does something unnatural) so is not within the same element. A click event will fire on the body (which in your example is the "most specific ancestor" referred to on MDN), but that won't be terribly helpful to you to listen for - because a click on the body can happen in many other ways, and you can't event use the event's target property to see where it originated, because in this case that is the body itself as explained.

If you really need this effect then use the mouseDown or mouseUp event rather than click, depending on what feels most natural to you. But the real answer is not to do this, as it is likely to be unexpected to your users and annoy them.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda