1

Есть такой код в HTML:

<div id="slider">
  <img src="images/user1.jpg" style="opacity:0.5" width="90px" height="90px">
  <img src="images/user2.jpg" width="90px" height="90px">
  <img src="images/user3.jpg" style="opacity:0.5" width="90px" height="90px">
</div>

Как сделать из этих изображений массив на JS/JQ?

1
  • Всем Большое спасибо!
    – Andy Krish
    21 окт 2017 в 10:44

2 ответа 2

4
  1. JavaScript

var container = document.getElementById("slider");
console.log(Array.from(container.children));
<div id="slider">
  <img src="images/user1.jpg" style="opacity:0.5" width="90px" height="90px">
  <img src="images/user2.jpg" width="90px" height="90px">
  <img src="images/user3.jpg" style="opacity:0.5" width="90px" height="90px">
</div>

  1. jQuery

console.log($("#slider").children().toArray())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="slider">
  <img src="images/user1.jpg" style="opacity:0.5" width="90px" height="90px">
  <img src="images/user2.jpg" width="90px" height="90px">
  <img src="images/user3.jpg" style="opacity:0.5" width="90px" height="90px">
</div>

0
var imgArray = [];

$.each($('div#slider img'), function (index, element){
 imgArray.push($(element).attr('src'));
});

console.log(imgArray);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.