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

317
Views
¿Cómo se puede optimizar este código para que tenga menos líneas de código y sea más conciso?

Quiero hacer este código más corto. ¿Como un bucle o algo así? Sería genial si alguien me ayudara a acortar el código para poder agregar más nth-child. como nth-child:4, 5.

 $(document).ready(function(){ var src=$(".gallery-grid-item:nth-child(1) img").data("src"); $.ajax({ url:src, xhrFields:{ responseType:'blob' }, success:function(data){ var url=window.URL||window.webkitURL; $(".gallery-grid-item:nth-child(1) img").attr("src",url.createObjectURL(data)); } }); }); $(document).ready(function(){ var src=$(".gallery-grid-item:nth-child(2) img").data("src"); $.ajax({ url:src, xhrFields:{ responseType:'blob' }, success:function(data){ var url=window.URL||window.webkitURL; $(".gallery-grid-item:nth-child(2) img").attr("src",url.createObjectURL(data)); } }); }); $(document).ready(function(){ var src=$(".gallery-grid-item:nth-child(3) img").data("src"); $.ajax({ url:src, xhrFields:{ responseType:'blob' }, success:function(data){ var url=window.URL||window.webkitURL; $(".gallery-grid-item:nth-child(3) img").attr("src",url.createObjectURL(data)); } }); });
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

<script> $(document).ready(function(){ numberOfNthChilds = 5 for (let i = 1; i < numberOfNthChilds + 1; i++) { var src=$(".gallery-grid-item:nth-child(" + i + ") img").data("src"); $.ajax({ url:src, xhrFields:{ responseType:'blob' }, success:function(data){ var url=window.URL||window.webkitURL; $(".gallery-grid-item:nth-child(" + i + ") img").attr("src",url.createObjectURL(data)); } }); } }); </script>
over 4 years ago · Santiago Trujillo Report

0

Prueba esto:

 <script> $(document).ready(function(){ for (var i = 1; i <= 3; i++) { var cls = ".gallery-grid-item:nth-child(" + i +") img"; var src=$(cls).data("src"); $.ajax({ url:src, xhrFields:{ responseType:'blob' }, success:function(data){ var url=window.URL||window.webkitURL; $(cls).attr("src",url.createObjectURL(data)); } }); } }); </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!