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

169
Views
Encuentre un enlace basado en URL específico y agregue valor adicional en los atributos rel en el sitio web completo a través de jquery

Quiero agregar valor adicional a los atributos donde todos los enlaces eran rel='nofollow' y href='example.com'. Para obtener más detalles, adjunto mi código a continuación.

Mi salida esperada:

 <a target="_blank" rel="nofollow sponsored" class="" href="https://amzn.to/tes" style="width: 100%;">Aff Link</a> <a target="_blank" rel="follow" href="example.com">Follow</a>

Mi código a continuación:

 var test = jQuery('a').attr('href').split('/')[2]; if (test == 'amzn.to') { jQuery('a').attr('rel', 'nofollow sponsored'); console.log(test); }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>jQuery Rel Attribute</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <a href="example.com">Example link</a> <br> <a target="_blank" rel="nofollow" class="" href="https://amzn.to/tessdft" style="width: 100%;"> Link 1 </a> <br> <a target="_blank" rel="nofollow" class="" href="https://amzn.to/testws" style="width: 100%;"> Link 2 </a> <br> <a target="_blank" rel="nofollow" class="" href="https://amzn.to/testee" style="width: 100%;"> Link </a> <br> </body> </html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tu linea

 jQuery('a').attr('href')

solo devolverá el href para la primera a ; debe repetir cada a :

 console.log("before: " + $("a[rel*=sponsored]").length) $("a").each(function() { var test = $(this).attr('href').split('/')[2]; if (test == 'amzn.to') { $(this).attr('rel', 'nofollow sponsored'); console.log(test); } }); console.log("after: " + $("a[rel*=sponsored]").length)
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <a href="example.com">Example link</a> <br> <a target="_blank" rel="nofollow" href="https://amzn.to/tessdft">Link 1</a><br> <a target="_blank" rel="nofollow" href="https://amzn.to/testws">Link 2</a><br> <a target="_blank" rel="nofollow" href="https://amzn.to/testee">Link3</a><br>

También hay una sobrecarga para .attr que le permite pasar una función

 console.log("before: " + $("a[rel*=sponsored]").length) $("a").attr("rel", function() { var test = $(this).attr('href').split('/')[2]; if (test == 'amzn.to') { return 'nofollow sponsored'; } }); console.log("after: " + $("a[rel*=sponsored]").length)
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <a href="example.com">Example link</a> <br> <a target="_blank" rel="nofollow" href="https://amzn.to/tessdft">Link 1</a><br> <a target="_blank" rel="nofollow" href="https://amzn.to/testws">Link 2</a><br> <a target="_blank" rel="nofollow" href="https://amzn.to/testee">Link3</a><br>

about 4 years ago · Juan Pablo Isaza 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!