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)); } }); });<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>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>