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

270
Views
¿Cómo creo un enlace usando javascript?

Tengo una cadena para un título y una cadena para un enlace. No estoy seguro de cómo juntar los dos para crear un enlace en una página usando Javascript. Cualquier ayuda es apreciada.

EDIT1: Agregar más detalles a la pregunta. La razón por la que estoy tratando de resolver esto es porque tengo un feed RSS y tengo una lista de títulos y URL. Me gustaría vincular los títulos a la URL para que la página sea útil.

EDIT2: estoy usando jQuery pero soy completamente nuevo y no sabía que podría ayudar en esta situación.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

<html> <head></head> <body> <script> var a = document.createElement('a'); var linkText = document.createTextNode("my title text"); a.appendChild(linkText); a.title = "my title text"; a.href = "http://example.com"; document.body.appendChild(a); </script> </body> </html>
over 4 years ago · Santiago Trujillo Report

0

con JavaScript

  1.  var a = document.createElement('a'); a.setAttribute('href',desiredLink); a.innerHTML = desiredText; // apend the anchor to the body // of course you can append it almost to any other dom element document.getElementsByTagName('body')[0].appendChild(a);
  2.  document.getElementsByTagName('body')[0].innerHTML += '<a href="'+desiredLink+'">'+desiredText+'</a>';

    o, como sugiere @travis :

     document.getElementsByTagName('body')[0].innerHTML += desiredText.link(desiredLink);
  3.  <script type="text/javascript"> //note that this case can be used only inside the "body" element document.write('<a href="'+desiredLink+'">'+desiredText+'</a>'); </script>

Con JQuery

  1.  $('<a href="'+desiredLink+'">'+desiredText+'</a>').appendTo($('body'));
  2.  $('body').append($('<a href="'+desiredLink+'">'+desiredText+'</a>'));
  3.  var a = $('<a />'); a.attr('href',desiredLink); a.text(desiredText); $('body').append(a);

En todos los ejemplos anteriores, puede agregar el ancla a cualquier elemento, no solo al 'cuerpo', y desiredLink es una variable que contiene la dirección a la que apunta su elemento ancla, y desiredText es una variable que contiene el texto que será que se muestra en el elemento de anclaje.

over 4 years ago · Santiago Trujillo Report

0

Crear enlaces usando JavaScript:

 <script language="javascript"> <!-- document.write("<a href=\"www.example.com\">"); document.write("Your Title"); document.write("</a>"); //--> </script>

O

 <script type="text/javascript"> document.write('Your Title'.link('http://www.example.com')); </script>

O

 <script type="text/javascript"> newlink = document.createElement('a'); newlink.innerHTML = 'Google'; newlink.setAttribute('title', 'Google'); newlink.setAttribute('href', 'http://google.com'); document.body.appendChild(newlink); </script>
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!