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

151
Views
¿Un código con "getElementsByClassName( )[ ]" entre corchetes? ¿Qué hace? ¿Cómo se puede convertir a jQuery?

Tengo un código en el fragmento que tiene document.getElementsByClassName("close")[0] , ¿qué está haciendo [0] ?

Nunca vi que se usaran corchetes en getElementsByClassName ¿para qué propósito se usa?

Además, ¿cómo puedo convertirlo a jQuery?

 var modal = document.getElementById("myModal"); var btn = document.getElementById("myBtn"); var span = document.getElementsByClassName("close")[0]; btn.onclick = function() { modal.style.display = "block"; } span.onclick = function() { modal.style.display = "none"; } window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } }
 body {font-family: Arial, Helvetica, sans-serif;} .modal { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); } .modal-content { background-color: #fefefe; margin: auto; padding: 20px; border: 1px solid #888; width: 80%; } .close { color: #aaaaaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; }
 <h2>Modal </h2> <button id="myBtn">Open Modal</button> <div id="myModal" class="modal"> <div class="modal-content"> <span class="close">&times;</span> <p>Some text in the Modal..</p> </div> </div>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

document.getElementsByClassName devuelve un objeto de elementos similar a una matriz.

[0] toma el primer elemento de esa matriz.

Básicamente:

 let elements = document.getElementsByClassName('close'); let span = elements[0];
about 4 years ago · Santiago Trujillo 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!