You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!DOCTYPE html><html><head><title>Exemplo de addEventListener</title></head><body><buttonid="meuBotao">Clique em Mim</button><pid="mensagem">O botão já foi clicado? Não.</p><script>// Acesse o botão pelo ID usando document.getElementById.constbotao=document.getElementById("meuBotao");// Acesse o parágrafo pelo ID onde exibiremos a mensagem.constmensagemParagrafo=document.getElementById("mensagem");// Adicione um ouvinte de evento de clique ao botão usando addEventListener.botao.addEventListener("click",function(){mensagemParagrafo.textContent="O botão já foi clicado? Sim.";console.log('Botão foi clicado com sucesso!');});</script></body></html>
2
<!DOCTYPE html><html><head><title>Exemplo de addEventListener com o Evento "change"</title></head><body><inputtype="text" id="meuInput" placeholder="Digite algo"><pid="mensagem"></p><script>// Acesse o campo de entrada de texto e o parágrafo pelo ID.constinput=document.getElementById("meuInput");constmensagemParagrafo=document.getElementById("mensagem");// Adicione um ouvinte de evento "change" ao campo de entrada de texto.input.addEventListener("change",function(){mensagemParagrafo.textContent="Valor atualizado: "+input.value;console.log(`Valor digitado: ${input.value}`);});</script></body></html>
3
<!DOCTYPE html><html><head><title>Exemplo de event.preventDefault()</title></head><body><p>Clique no link a seguir para ver o comportamento padrão:</p><ahref="https://www.example.com" id="meuLink">Exemplo.com</a><script>// Acesse o link pelo ID usando document.getElementById.constlink=document.getElementById("meuLink");// Adicione um ouvinte de evento de clique ao link.link.addEventListener("click",function(event){// Use event.preventDefault() para cancelar o comportamento padrão do link.event.preventDefault();console.log("O comportamento padrão do link foi cancelado. Você permanece na mesma página.");});</script></body></html>
4
<!DOCTYPE html><html><head><title>Exemplo de Evento de Hover</title><style>
#meuDiv {
width: 200px;
height: 200px;
background-color: #3498db;
margin: 20px;
}
</style></head><body><divid="meuDiv"></div><script>// Acesse o link pelo ID usando document.getElementById.constmeuDiv=document.getElementById('meuDiv');// Adicione um ouvinte de evento de mouseover à div.meuDiv.addEventListener('mouseover',function(){meuDiv.style.backgroundColor='#e74c3c';});// Adicione um ouvinte de evento de mouseout à div.meuDiv.addEventListener('mouseout',function(){meuDiv.style.backgroundColor='#3498db';});</script></body></html>