Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Выбрать все атрибуты (https://javascript.ru/forum/jquery/23881-vybrat-vse-atributy.html)

ermax 10.12.2011 01:30

Выбрать все атрибуты
 
Есть страница
<div class="map-locations" data-map="18.2065,-67.1566">
4 Store
</div><br />
<div class="map-locations" data-map="42.175642,-72.576486">
5 Store
</div><br />
<div class="map-locations" data-map="Champ de Mars 75007 Paris, France">6 6Store
</div>


Есть код JS
var dataLatLng = $("body").find("div.map-locations").attr("data-map");
   alert(dataLatLng);


Но мне возвращает только первый вариант 18.2065,-67.1566
Как сделать чтобы все данные записывались в масив?

devote 10.12.2011 01:59

<div class="map-locations" data-map="18.2065,-67.1566">
4 Store
</div><br />
<div class="map-locations" data-map="42.175642,-72.576486">
5 Store
</div><br />
<div class="map-locations" data-map="Champ de Mars 75007 Paris, France">6 6Store
</div>

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var latlng = [];
$("body").find("div.map-locations").each(function(){
    latlng.push( $( this ).attr("data-map") );
});

alert( latlng.join( "\n" ) );
</script>

ermax 10.12.2011 02:01

Большое спасибо. :victory:

denisOgr 13.12.2011 03:42

Вам нужно было:
1. Пройтись по всему массиву с выбранными элементами each()
2. Сформировать новый массив только из атрибтов data-map .push()
3. При выводе alert, разделить элементы массива \n join()

В помощь :victory:


Часовой пояс GMT +3, время: 09:12.