Soy realmente nuevo en JS e intenté buscar en Google pero no ayudó. Todo lo que estoy tratando de hacer es agregar el enlace Publicar medio en mi texto como un hipervínculo. Estoy bastante seguro de que estoy a mitad de camino, pero no puedo entenderlo:
_$ = document.querySelector.bind(document) ; var AppendLinkHere = _$("body") // <- put in here some CSS selector that'll be more to your needs var a = document.createElement('a'); a.text = "Medium Post"; a.href = "https://medium.com/@FundsFi/fundsfi-presale-terms-are-here-whitelisting-requirements-fc798d85b284"; // AppendLinkHere.appendChild(a) // a.title = 'Well well ... // a.setAttribute('title', 'Well well that\'sa link'); <s.TextDescription style={{ textAlign: "center", color: "var(--primary-text)", }}> blah blah blah blah blah blah blah blah blah blah Medium Post. </s.TextDescription>Todo lo que quiero hacer es que cuando las personas hagan clic en la palabra Publicación mediana, los lleve a una nueva pestaña con el enlace mediano. Además, obtuve el código de esta publicación mientras buscaba una respuesta: ¿Cómo creo un enlace usando javascript? Y luego me di cuenta de que necesito agregar un selector de CSS que no sé cómo hacerlo. Hasta ahora encontré esta publicación sobre: ¿Cómo se agrega CSS con Javascript?
Si quieres que sea javascript, esto podría ayudar.
// create anchor link element let link = document.createElement("a") // Create txt let txt = document.createTextNode("Medium Post") //append txt to anchor element link.appendChild(txt) // set the title link.title ="Medium Post"; // set the href property link.href = "https://www.medium.com"; // get text to add link to let el = document.getElementById("p") // append the link to the el id = "p" el.appendChild(link) <div class="textDescription"> <p id="p">blah blah blah blah blah blah blah blah blah blah </p> </div>Espero que esto ayude, avísame si tienes alguna pregunta.
Por lo que entiendo, desea que una página HTML incluya un texto en el que un usuario pueda hacer clic para ir a un nuevo laboratorio que tenga su enlace, bueno, hay muchas maneras en que puede hacer esto, incluida la escritura en código HTML en lugar de javascript para será <a href="link here" target="_blank">medium</a> y mostrará un texto de hipervínculo en la página que, al hacer clic, lo llevará a una nueva página o si desea usar JavaScript puede usar document.querySelector("id here").innerHTML = '<a href="link here" target="_blank">medium</a>'; y eso le permitiría mostrar el enlace dentro de la página y el enlace en un evento si se configura con document.addEventListener pero tenga en cuenta que eliminará todo el código, el texto y las etiquetas dentro de la etiqueta seleccionada y colocará el enlace. Lo siento si me equivoqué en algo, todavía soy un poco nuevo: D
En la solución a continuación, el elemento <div> con un valor de id de link se agrega al elemento <a> cuando ocurre el evento de load de la página.
let linkElement = document.getElementById('link'); const targetAddress = "https://medium.com/@FundsFi/fundsfi-presale-terms-are-here-whitelisting-requirements-fc798d85b284"; /* The load event is triggered when the page loads. */ window.onload = function() { /* The new <a> element is created. */ var newLinkElement = document.createElement('a'); /* A new text node is created. */ var newContent = document.createTextNode("Medium Post"); /* The text node is added to the <a> element. */ newLinkElement.appendChild(newContent); /* The class attribute of the <a> element is assigned the value "aElementStyle". */ newLinkElement.setAttribute('class', 'aElementStyle'); /* The targetAddress variable content is assigned to the href attribute of the <a> element. */ newLinkElement.setAttribute('href', targetAddress); /* The <a> element is linked as a child to the <div> element whose id value is "link". */ linkElement.appendChild(newLinkElement); } .aElementStyle { color: white; background-color: black; text-decoration: none; } <body> <div id="link"></div> </body>