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

264
Views
Obtenga el índice del elemento como hijo en relación con el padre

Digamos que tengo este marcado:

 <ul id="wizard"> <li>Step 1</li> <li>Step 2</li> </ul>

Y tengo este jQuery:

 $("#wizard li").click(function () { // alert index of li relative to ul parent });

¿Cómo puedo obtener el índice del hijo li en relación con su padre, al hacer clic en ese li ?

Por ejemplo, cuando hace clic en "Paso 1", debería aparecer una alert con "0".

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

$("#wizard li").click(function () { console.log( $(this).index() ); });

Sin embargo, en lugar de adjuntar un controlador de clic para cada elemento de la lista, es mejor (en cuanto al rendimiento) usar el delegate que se vería así:

 $("#wizard").delegate('li', 'click', function () { console.log( $(this).index() ); });

En jQuery 1.7+, debe usar on . El siguiente ejemplo vincula el evento al elemento #wizard , que funciona como un evento de delegado:

 $("#wizard").on("click", "li", function() { console.log( $(this).index() ); });
over 4 years ago · Santiago Trujillo Report

0

algo como:

 $("ul#wizard li").click(function () { var index = $("ul#wizard li").index(this); alert("index is: " + index) });
over 4 years ago · Santiago Trujillo Report

0

Echa un vistazo a este ejemplo .

 $("#wizard li").click(function () { alert($(this).index()); // alert index of li relative to ul parent });
over 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!