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

213
Views
Agregar href al ancla usando JavaScript

Creé una barra de menú y creé dinámicamente el li y el ancla usando js. Ahora puedo agregar el href y el texto usando JavaScript a cada una de las listas creadas:
<li><a></li></a> <li><a></li></a>.. así que al hacer clic en cualquiera de los elementos de la lista, se mueve a la sección vinculada en la página.

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

0

Seleccione e itere sobre sus li>a s y establezca su href y cualquier otro atributo que desee. Algo como abajo

 const links = [{ title: 'Google', link: 'google.com' }, { title: 'Yahoo', link: 'yahoo.com' }, ]; document.querySelectorAll('li>a').forEach((itm, index) => { if (index >= links.length) return; itm.href = links[index].link; itm.text = links[index].title })
 <ul> <li> <a /> </li> <li> <a /> </li> </ul>

O puede agregar li s dinámicamente de acuerdo con su conjunto de enlaces.

 const links = [{ title: 'Google', link: 'google.com' }, { title: 'Yahoo', link: 'yahoo.com' }, ]; let ul = document.getElementById('nav'); links.forEach(itm => { let li = document.createElement("li"); let link = document.createElement("a"); link.href = itm.link; link.text = itm.title li.appendChild(link) ul.appendChild(li) })
 <ul id="nav"></ul>

about 4 years ago · Juan Pablo Isaza Report

0

Primero, debe otorgar a cada elemento una identificación única. Luego use su id como referencia y agregue su atributo href.

Consideremos que tu elemento es

 <li><a id='list1'>Stackoverflow</a></li>

Y el código js será

 <script> document.getElementById("list1").href = "https://stackoverflow.com"; </script>
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!