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

283
Views
JQuery cambia el texto interno pero conserva html

Me gustaría cambiar el texto de un elemento HTML pero preservar el resto del html interno con jQuery.

Por ejemplo:

 <a href="link.html">Some text <img src="image.jpg" /></a>

reemplace "Algún texto" con "Otro texto", y el resultado debería verse así:

 <a href="link.html">Other text <img src="image.jpg" /></a>

EDITAR: Mi solución actual es la siguiente:

 var aElem = $('a'); var children = aElem.children(); aElem.text("NEW TEXT"); aElem.append(children);

Pero debe haber alguna manera más elegante de hacer esto.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Esto parece funcionar bien.

Demo en vivo

 <html> <head> </head> <body> <a href="link.html">Some text <img src="img.jpg" /></a> </body> </html> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script type="text/javascript"> $(function(){ var $link = $('a'); var $img = $link.find('img'); $link.html('New Text'); $link.append($img); }); </script>
over 4 years ago · Santiago Trujillo Report

0

Como no puede modificar el enlace, una opción sería simplemente usar replace

 $("a").html($("a").html().replace("Some text", "Other text"));

Ejemplo en jsfiddle .

over 4 years ago · Santiago Trujillo Report

0

Envuelva el texto que desea cambiar en un lapso

 <a href="link.html"><span>Some text</span> <img src="image.jpg" /></a> $('a span').html( 'new text' );
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!