Soy totalmente nuevo en javascript y estoy tratando de crear una lista dinámica en JavaScript que sea un hipervínculo y muestre la cadena de texto de una etiqueta en mi código html que tiene el id = v1. Algo así como un menú a diferentes partes de esa misma página web.
He intentado hacer esto usando fragmentos de documentos ( https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Creating_hyperlinks#document_fragments ) pero no funciona y no tengo ni idea de cómo insertar la cadena de texto del en el "menú".
Aquí está parte de mi código:
var container = document.getElementById("contentarea"); var header = document.getElementById("v1"); for (i = 0; i < length.header; i++) { $(document.createElement("li")).li; $(document.createElement("a")).link.append(li)( { href: "#v1" }) list.appendChild(li); container.appendChild(list) }Podrías hacer algo como esto
// div to get the textContent from, replace it with whatever you want const divE = document.getElementById('test'); // our list tag const list = document.getElementById('list'); // looping for (let i = 0; i < 4; i++) { const listEl = document.createElement('li'); // our list element const aTag = document.createElement('a'); // a new <a> tag to append link const linkTag = document.createElement('link'); //new link tag linkTag.href = '#test'; // setting hyperlink aTag.textContent = divE.textContent // setting the textContent for <a> tag aTag.appendChild(linkTag); // appending link to the a tag listEl.appendChild(aTag); // appending a tag to the <li> tag list.appendChild(listEl) // finally appending our list element to the main list } <div id="test">Something here</div> <ul id="list"></ul>