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

170
Visualizações
How to click an html box without registering the click event of the box behind it?

I have one box placed on top of another box both having their own separate click events. The box on top is blue and the one behind it is red. Whenever I click on the blue box the red box's click event also registers. Here's the code. Also please do not use event.preventDefault(); event.stopPropagation(); if you're answering as it has given me trouble in the past.

box1() {
  console.log("box 1");   
}
box2() {
  console.log("box 2");
}
.box1{
    left : 100px;
    top: 100px;
    width: 400px;
    height: 400px;
    background-color: red;

}
.box2{
    position: relative;
    left : 100px;
    top: 100px;
    width: 400px;
    height: 400px;
    background-color:blue;
}
<div class="container">
    <div class="box1" (click)="box1()">
        <div class="box2" (click)="box2()">
        </div>
    </div>
</div>

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I have reviewed your code and i have changed your css & html little bit and it works like a charm.

box.html

<div class="container">
   <div class="box1" (click)="box1()"></div>
   <div class="box2" (click)="box2()"></div>
</div>

box.css

.box1 {
  position: fixed;
  left: 100px;
  top: 100px;
  width: 400px;
  height: 400px;
  background-color: red;
  z-index: 0;
}
.box2 {
  position: fixed;
  left: 200px;
  top: 200px;
  width: 400px;
  height: 400px;
  background-color: blue;
  z-index: 999;
}
.container {
  position: relative;
  left: 100px;
  top: 100px;
}

box.ts

 box1() {
    console.log("box 1");   
  }
  box2() {
    console.log("box 2");
  }

working demo: https://stackblitz.com/edit/angular-ivy-rxyqcd?file=src/app/app.component.ts

over 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