Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

357
Views
¿Cómo agregar el detector de eventos javascript onclick al contenedor div?

Agregue el detector de eventos onclick al contenedor div.

Intenté lo siguiente y, como puede ver, el evento no parece estar registrado. ¿Cómo puedo agregar el oyente a este div?

El ID de div que quiero es "engineersContainer".

 document.getElementById("engineersContainer").addEventListener("click", function(e) { console.log("It was clicked"); alert("It was clicked"); });
 .barContainer { margin: 2px; border: 2px solid black; border-radius: 20px; width: 200px; //width:-moz-fit-content; //width: fit-content; padding: 5px; overflow: hidden; position: relative; background: #34e8eb; z-index: -1; } .containerHeader:before { content:""; background: skyblue; position: absolute; inset: 0; z-index: -1; height:45px }
 <div id="engineersContainer" class="barContainer"> <div id="engineerList" class="containerHeader" style="font-size:1.5em; text-align:center">Current Engineer </div> <div> Engineer's Name</div> <div id="clickText" style="padding-bottom: 10px; font-size:0.75em; text-align:center "> (Click to See All Permits) </div> </div>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  • Elimine el índice z -1; de lo contrario, estará debajo del resto de la página
  • Agregue el detector de eventos en el evento de carga de la página

Vea el segundo ejemplo para una solución

 window.addEventListener("load", function() { document.getElementById("engineersContainer") .addEventListener("click", function(e) { console.log("It was clicked"); }); });
 .barContainer { margin: 2px; border: 2px solid black; border-radius: 20px; width: 200px; //width:-moz-fit-content; //width: fit-content; padding: 5px; overflow: hidden; position: relative; background: #34e8eb; /* z-index: -1 */ } .containerHeader:before { content: ""; background: skyblue; position: absolute; inset: 0; z-index: -1; height: 45px } .containerHeader { font-size: 1.5em; text-align: center } #clickText { padding-bottom: 10px; font-size: 0.75em; text-align: center }
 <div id="engineersContainer" class="barContainer"> <div id="engineerList" class="containerHeader">Current Engineer </div> <div> Engineer's Name</div> <div id="clickText"> (Click to See All Permits) </div> </div>

De lo contrario, agregue otro div encima

 window.addEventListener("load", function() { document.getElementById("engineersContainerClick").addEventListener("click", function(e) { console.log("It was clicked"); }); });
 .barContainer { margin: 2px; border: 2px solid black; border-radius: 20px; width: 200px; //width:-moz-fit-content; //width: fit-content; padding: 5px; overflow: hidden; position: relative; background: #34e8eb; z-index: -1 } #engineersContainerClick { margin: 2px; border-radius: 20px; width: 200px; height:70px; padding: 5px; border:none; overflow: hidden; position: absolute;top:0; background-color: transparent; z-index:999; } .containerHeader:before { content: ""; background: skyblue; position: absolute; inset: 0; z-index: -1; height: 45px } .containerHeader { font-size: 1.5em; text-align: center } #clickText { padding-bottom: 10px; font-size: 0.75em; text-align: center }
 <div id="engineersContainer" class="barContainer"> <div id="engineerList" class="containerHeader">Current Engineer </div> <div> Engineer's Name</div> <div id="clickText"> (Click to See All Permits) </div> </div> <div id="engineersContainerClick" class="barContainer">

about 4 years ago · Juan Pablo Isaza Report

0

 document.getElementById("engineersContainer").onclick=function(){ console.log("your event"); }
 .barContainer { margin: 2px; border: 2px solid black; border-radius: 20px; width: 200px; //width:-moz-fit-content; //width: fit-content; padding: 5px; overflow: hidden; position: relative; background: #34e8eb; } .containerHeader:before { content:""; background: skyblue; position: absolute; inset: 0; z-index: -1; height:45px }
 <body> <div id="engineersContainer" class="barContainer"> <div id="engineerList" class="containerHeader" style="font-size:1.5em; text-align:center">Current Engineer </div> <div> Engineer's Name</div> <div id="clickText" style="padding-bottom: 10px; font-size:0.75em; text-align:center "> (Click to See All Permits) </div> </div> </body>

ng-js -->

 document.getElementById("engineersContainer").onclick=function(){ console.log("your event"); }
 <div id="engineersContainer"> Here is the Engineers Container </div>

Usaría una función anónima para esto. Debe llamar al script después de que se haya creado su div, ya que Javascript se lee de arriba a abajo y no tendrá una referencia si se ejecuta antes de que se haya colocado el div.

Tenga en cuenta que si tiene un oyente en el contenedor div, afectará su capacidad para introducir cualquier opción en la que se pueda hacer clic dentro de los elementos secundarios de divs, como botones más adelante. ¿Podría esto causarle problemas en el futuro? Si es así, reconsidere el diseño posiblemente para ahorrarle algunos problemas más adelante. Además, el índice Z negativo debe eliminarse, ya que se dibuja debajo de la capa que Javascript detecta los clics. Perdón por las ediciones, soy nuevo en StackO.

about 4 years ago · Juan Pablo Isaza Report

0

Debe eliminar el z-index: -1 en su CSS.

Los clics de los usuarios no hacen contacto con el elemento, sino con el documento que se encuentra sobre el elemento.


Ejemplo de trabajo:

 document.getElementById("engineersContainer").addEventListener("click", function(e) { console.log("It was clicked"); alert("It was clicked"); });
 .barContainer { margin: 2px; border: 2px solid black; border-radius: 20px; width: 200px; /* width:-moz-fit-content; */ /* width: fit-content; */ padding: 5px; overflow: hidden; position: relative; background: #34e8eb; } .containerHeader:before { content:""; background: skyblue; position: absolute; inset: 0; z-index: -1; height:45px }
 <div id="engineersContainer" class="barContainer"> <div id="engineerList" class="containerHeader" style="font-size:1.5em; text-align:center">Current Engineer </div> <div> Engineer's Name</div> <div id="clickText" style="padding-bottom: 10px; font-size:0.75em; text-align:center "> (Click to See All Permits) </div> </div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!