0

場所のリストを取得する検索アプリケーションがあります。これらの場所と名前は、次のような属性タグに含まれます

<div data-location="22.4245,-15.000" data-id="place_name_1">Place Name 1</div>
<div data-location="23.4435,-13.000" data-id="place_name_2">Place Name 2</div>
<div data-location="27.42755,-13.000" data-id="place_name_3">Place Name 3</div>

そして、私はこの情報を使用して、Googleマップでマーカーを取得しています。私は彼らのドキュメントを見ています、そして私はどうやってこの情報をjavascriptの配列に入れるのか疑問に思いましたか?リンクを見ると、次のような配列があります。

var beaches = [ 
  ['Bondi Beach', -33.890542, 151.274856, 4], 
  ['Coogee Beach', -33.923036, 151.259052, 5], 
  ['Cronulla Beach', -34.028249, 151.157507, 3], 
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], 
  ['Maroubra Beach', -33.950198, 151.259302, 1] 
]; 

htmlからjavascriptの配列にデータを取得するには何をする必要がありますか?

ありがとう!

4

3 に答える 3

1

これを試して:

var $beaches = [];

$.find("div").each(function(){

     var $loc = [];

    $loc.push($(this).html());
    $loc.push($(this).attr("data-location"));
    $loc.push($(this).attr("data-id"));
    $loc.push(2);

    $beaches.push($loc);
 });
于 2012-11-21T07:40:09.527 に答える
0

jQueryを使用すると、HTMLを解析できます。

var beaches = [];

// iterate over all <div>s (assuming they're always in that format)
$('div').each(function (index, div) {

    // break apart the comma-separated coordinates in the data-location attribute
    var coords = $(div).data('location').split(',');

    // add this location to the beaches array (with z-index 0)
    beaches.push([$(div).text(), coords[0], coords[1], 0]);

});

一部のアプリケーションでは、座標を経度、緯度としてエンコードする場合があります。その場合は、の位置を入れ替える必要がcoordsあります。つまり、beaches.push([$(div).text(), coords[1], coords[0], 0]);

setMarkers()配列は、参照している例(実際には関数)に非常に固有であることに注意してください。

于 2012-11-21T07:30:02.390 に答える
0
var beaches = [];    
$('div[data-location][data-id]').each(function() {
    beaches.push([
        [$(this).attr('data-id')]
            .concat(
                $(this).attr('data-location').split(","),
                [2]
            )
    ]);
});
于 2012-11-21T07:26:59.337 に答える