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

157
Visualizações
Is there any way to check if an element has any changes on mouseover by javascript?

I want to detect if an element has any changes, such as the background-color changes to another or cursor changes to pointer, on mouseover by javascript.

I thought about using getComputedStyle(element, ":hover") but this is not available with :hover pseudo.

I also tried to simulate mouse over by jQuery like below, but it seems like it does not work as I expected.

$obj.css('cursor')
$obj.mouseover().css('cursor')

Is there any good way?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You could add an eventlistener, such as "mouseenter" and then console log every time the mouse hovers over an element.

If you test out this code, you will see that the number of times you have hovered over the list items will appear in the console.

here is a link to "mouseover" event documentation, which I used and edited to help simplify my answer.

https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseover_event

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>


  <ul id="test">
    <li>Check if item has been hovered over</li>
    <li>item 2</li>
  </ul>


  <script>
    let test = document.getElementById("test");
    // This handler will be executed only once when the cursor moves over the unordered list
    test.addEventListener("mouseenter", function(event) {
      // highlight the mouseenter target
      console.log("purple has been hovered over")
      event.target.style.color = "purple";
      // reset the color after a short delay
      setTimeout(function() {
        event.target.style.color = "";
      }, 500);
    }, false);
  </script>

</body>

</html>

about 4 years ago · Santiago Trujillo Relatório

0

Unfortunately, you can't do it via jQuery like that. You probably need to get the stylesheets for the document and manually iterate through them to find matches to your selector.

function getCss(cssSelector){
    var styleSheets = document.styleSheets;
    var matchedRules = [];
    for(var i = 0; i < styleSheets.length; i++) {
        var rules = styleSheets[i].rules || sheets[i].cssRules;
        for (var j = 0; j < rules.length; j++) {
            if (rules[j].selectorText === cssSelector) {
                matchedRules.push(rules[j].cssText);
            }
        }
    }
    return matchedRules;
}

You can then call the function to obtain the matched rules. For example:

getCss("a:hover");
about 4 years ago · Santiago Trujillo 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