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

107
Visualizações
Jquery get image src array

I'm making a discord bot with discord.js. jQuery get the image src

looked for the most relevant article, but it's different from what I'm trying to do.

I'd like to receive several srcs of img in the span tag by array.

<div class="jewel__wrap">
   <span id="gem00" class="jewel_btn" data-grade="3" data-item="E595062cGem_000">
     <span class="jewel_img">
       <img src="https://xxx/60.png" alt=""> // this src
     </span>
     <span class="jewel_level">Lv.5</span>
   </span>
   <span id="gem01" class="jewel_btn" data-grade="3" data-item="E595062cGem_001">
     <span class="jewel_img">
       <img src="https://xxx/50.png" alt=""></span> // this src
     <span class="jewel_level">Lv.5</span>
   </span>
</div>
const jewel = $(".jewel_level").text();
const jewel2 = $(".jewel_img").find('img').attr("src");

console.log("jewel : ", jewel, "jewel2 : ", jewel2);

jewel receives both lv.5 and ex) lv.5lv.5. jewel2 only receives one src.

How should I bring it?

console

jewel : Lv.5Lv.5 jewel2 : https://xxx/60.png
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem with jQuery's .attr() is...

Get the value of an attribute for the first element in the set of matched elements

Instead, you can use jQuery's .map() to transform a collection of elements into an array

const sources = $(".jewel_img > img")
  .map((_, { src }) => src) // extract property
  .get() // get the array out of the jQuery object
  
console.log("sources", sources)
<!-- minified HTML -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
<div class="jewel__wrap"> <span id="gem00" class="jewel_btn" data-grade="3" data-item="E595062cGem_000"> <span class="jewel_img"> <img src="https://xxx/60.png" alt=""> </span> <span class="jewel_level">Lv.5</span> </span> <span id="gem01" class="jewel_btn" data-grade="3" data-item="E595062cGem_001"> <span class="jewel_img"> <img src="https://xxx/50.png" alt=""></span> <span class="jewel_level">Lv.5</span> </span></div>


You may even want to treat the text in a similar way and collect all the data into objects

const data = $(".jewel_btn")
  .map((_, btn) => {
    const $btn = $(btn)
    return {
      image: $btn.find(".jewel_img img").attr("src"),
      level: $btn.find(".jewel_level").text()
    }
  })
  .get()
  
console.log("data", data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
<div class="jewel__wrap"> <span id="gem00" class="jewel_btn" data-grade="3" data-item="E595062cGem_000"> <span class="jewel_img"> <img src="https://xxx/60.png" alt=""> </span> <span class="jewel_level">Lv.5</span> </span> <span id="gem01" class="jewel_btn" data-grade="3" data-item="E595062cGem_001"> <span class="jewel_img"> <img src="https://xxx/50.png" alt=""></span> <span class="jewel_level">Lv.5</span> </span></div>

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