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

150
Views
jQuery para mover el texto alternativo solo a cada elemento hermano

Traté de jugar con .append() .insertAdjacentHTML() y .insertAfter() para mover el texto alternativo de cada imagen a su elemento hermano, pero no funciona como se esperaba...

El resultado objetivo es:
Imagen 1
Título 1
Imagen 2
Título 2

 var altText = $("img").attr("alt"); $("span#here").append(altText);
 .wrap { margin-bottom: 10px; } .demo-img { width: 280px; } span#here { display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrap"> <img src="https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE4Y5jq?ver=ac72&q=90&m=6&h=201&w=358&b=%23FFFFFFFF&l=f&o=t&aim=true" alt="Example Image Caption 1" class="demo-img"> <span id="here"></span> </div> <div class="wrap"> <img src="https://cdn-dynmedia-1.microsoft.com/is/image/microsoftcorp/gldn-M365-CP-Microsoft365-Commercial?wid=380&hei=213&fit=crop" alt="Example Image Caption 2" class="demo-img"> <span id="here"></span> </div>

Se lo agradezco mucho si alguien puede ayudar, ¡gracias!

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

No hay un forro para esto. Debe recorrer las imágenes y configurar el texto en el bucle.

 $(".wrap img").each(function (_, img) { const alt = img.alt; $(img).next("span").text(alt); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrap"> <img src="https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE4Y5jq?ver=ac72&q=90&m=6&h=201&w=358&b=%23FFFFFFFF&l=f&o=t&aim=true" alt="Example Image Caption 1" class="demo-img"> <span id="here"></span> </div> <div class="wrap"> <img src="https://cdn-dynmedia-1.microsoft.com/is/image/microsoftcorp/gldn-M365-CP-Microsoft365-Commercial?wid=380&hei=213&fit=crop" alt="Example Image Caption 2" class="demo-img"> <span id="here"></span> </div>

Otra forma es seleccionar los tramos y hacer referencia a la imagen.

 $(".wrap span").text(function() { return $(this).prev('img').attr('alt'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrap"> <img src="https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE4Y5jq?ver=ac72&q=90&m=6&h=201&w=358&b=%23FFFFFFFF&l=f&o=t&aim=true" alt="Example Image Caption 1" class="demo-img"> <span id="here"></span> </div> <div class="wrap"> <img src="https://cdn-dynmedia-1.microsoft.com/is/image/microsoftcorp/gldn-M365-CP-Microsoft365-Commercial?wid=380&hei=213&fit=crop" alt="Example Image Caption 2" class="demo-img"> <span id="here"></span> </div>

about 4 years ago · Santiago Gelvez 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!