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

209
Visualizações
How can I make the text invisible when hovering over a tex and pressing the mouse button?

I'm new, tell me how to implement so that when you hover and click on the mouse button, the text should be made invisible. And when you click on the spacebar, it will be deleted from the page. Can this be done with just HTML and CSS? Or can it be implemented using JavaScript? Tell me how to do it better? Here is the code where I implemented the text change:

.body {
  background: #f1f1f1;
  font-family: tahoma;
}

.container {
  width: 600px;
  margin: 70px auto;
}

.block {
  padding: 40px;
  border: 1px solid #ddd;
  color: #666;
  background: #fff;
}

.eng {
  color: lime;
  font-family: sans-serif;
  display: none;
}

.container_1:hover .rus {
  display: none;
}

.container_1:hover .eng {
  display: block;
}
<div class="container">
  <div class="container_1">
    <div class="block rus">
      Junior Frontend Developer
    </div>
    <div class="block eng">
      Junior Frontend Developer
    </div>
  </div>
</div>

Now you need to do it on mouse click and space, but I don’t really understand how to implement it

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

0

Well first, your current code for swapping the display of the message (while functional) is not really a good approach. Instead of having two pre-made versions of the text and then showing/hiding one vs. the other, just have the one element and change its style when needed.

As for making the element become hidden and then removed, see the following code with comments:

// Variable to store the last item that was clicked
let lastClicked = null;

// Set up a click event on the element
document.querySelector(".block.eng").addEventListener("click", function(evt){
  lastClicked = this; // Store a reference to what was just clicked
  this.classList.add("hidden"); // Add the CSS that hides the element
  
});

// Set up the spacebar press event
document.addEventListener("keypress", function(evt){
  if(evt.key === " "){       // Check for spacebar
    lastClicked.remove();    // Remove the element from the document
  }
});
.body {
    background: #f1f1f1;
    font-family: tahoma;
}

.container {
    width: 600px;
    margin: 70px auto;
}

.block {
    padding: 40px;
    border: 1px solid #ddd;
    color: #666;
    background: #fff;
}

.container_1:hover .eng{
    color: lime;
    font-family: sans-serif;
}

.hidden{
    opacity:0;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <link rel="stylesheet" href="style.css"type="text/css">
    </head>
    <body>
      <div class="container_1">       
        <div class="block eng">
            Junior Frontend Developer
        </div>test
      </div>
    </body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

If I understood what you needed done correctly. It can be done using some CSS and JS.

Here's a code snippet

$(".MagicElement").on("click", function (element) 
{
  $(this).toggleClass("hidden");
});

$('body').keyup(function(e){
   if(e.keyCode == 32){
       // user has pressed space
       $(".MagicElement:hover").remove();
   }
});
.MagicElement:active { opacity: 0; }
.hidden { opacity: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = 'MagicElement'>This is a block of text 1</div>
<div class = 'MagicElement'>This is a block of text 2</div>
<div class = 'MagicElement'>This is a block of text 3</div>
<div class = 'MagicElement'>This is a block of text 4</div>

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