Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
How can this code be optimised, so that it has fewer lines of code and is more consise?

I want to make this code shorter. Like a loop or something? It would be great if anyone helps me to make the code shorter so that I can add more nth-child. like 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));
        }
    });
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

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

0

Try this:

<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>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda