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

228
Visualizações
How to find all div which have style background-image?

Is there any jquery or JS to file all div that have background image. I need to lazy load on these div.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There is a good post that shows you how to find all images (images, background-images, images in iframes): https://blog.crimx.com/2017/03/09/get-all-images-in-dom-including-background-en/
So, if you are trying to find all divs that have a background-image that's an image from an URL (not a gradient), this code would do it for you:

let divs = document.querySelectorAll("div");

let urlRegex = /url\(\s*?['"]?\s*?(\S+?)\s*?["']?\s*?\)/;

var divsWithBackgroundImage = Array.from(divs).filter((div) => {
    let backgroundImage = getComputedStyle(div).getPropertyValue("background-image");
  
    return (backgroundImage.match(urlRegex));
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Another solution

$('div').each(function() {
    if($(this).css('background-image') != 'none'){
        console.log($(this).css('background-image').split(/"/)[1]);
    }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

I got another solution

$('div').filter(function(){
    if(this.style.backgroundImage){
        console.log(this.style.backgroundImage);
    }
});
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